synchronization

how to make a multithread copy files

有些话、适合烂在心里 提交于 2019-12-02 08:02:44
I want to copy many files in one, but using multiThread,supposing that file A is the file in which different threads copy datas, in this case each thread is meant to copy one file in file A, using this procedure: procedure ConcatenateFiles(const InFileNames: array of string; const OutFileName: string); var i: Integer; InStream, OutStream: TFileStream; begin OutStream := TFileStream.Create(OutFileName, fmCreate); try for i := 0 to high(InFileNames) do begin InStream := TFileStream.Create(InFileNames[i], fmOpenRead); try OutStream.CopyFrom(InStream, InStream.Size); finally InStream.Free; end;

Do async in a blocking program language way?

蓝咒 提交于 2019-12-02 07:41:52
Sync way For example, ruby: con = Mysql.new('localhost') rs1 = con.query('select * from test01') # A rs2 = con.query('select * from test02') # B rs = getResult(rs1, rs2) # C con.close so A will block the execution. B will be executed until A is done. So does C Async way for example, nodejs var mysql = require('mysql'); var connection = mysql.createConnection({host : 'localhost',}); connection.connect(); connection.query('SELECT * from test01', function(err, rows, fields) { console.log(rows); }); // A connection.query('SELECT * from test02', function(err, rows, fields) { console.log(rows); });

synchronizing by this vs dummy object

陌路散爱 提交于 2019-12-02 07:26:07
I have seen usage of synchronized block by this so far but recently I learned that using dummy object is preferable. I found the following topic related to this. Java synchronized method lock on object, or method? As a summary, in the code below, two different object can not run addA and addB concurrently as both uses this for lock. private int a; private int b; public synchronized void addA(){ a++; } public synchronized void addB(){ b++; } I am confused if I use dummy object for lock, what will be different if I use the line below in both method to synchronize? Because still they would have

The getAndIncrement implementation of AtomicInteger

无人久伴 提交于 2019-12-02 06:23:24
问题 The getAndIncrement implementation of AtomicInteger does the following: public final int getAndIncrement() { for (;;) { int current = get(); // Step 1 , get returns the volatile variable int next = current + 1; if (compareAndSet(current, next)) return current; } } Isn't it an equivalent of aVolatileVariable++? (which we know is not a correct usage). Without synchronization, how are we ensuring that this complete operation is atomic? What if the value of the volatile variable changes after the

Threading and synchronized methods

走远了吗. 提交于 2019-12-02 06:18:51
问题 I have the following code: public class MyThread extends Thread { private int i; public static int sum=0; public MyThread(int k){ i=k; } public static void main(String[] args) throws InterruptedException{ Thread t=new MyThread(1); Thread s=new MyThread(2); Thread p=new MyThread(3); t.start(); s.start(); } public synchronized void doSomething(){ for(int i=0; i<100000; i++){ System.out.println(this.i); } } @Override public void run() { doSomething(); } } the doSomething is synchronized. why is

Implementing Barrier in Python2.7

你说的曾经没有我的故事 提交于 2019-12-02 06:04:47
问题 I have this code implemented in Python3 using Barriers. I want to get the same functionality in Python2.7 but i don't know which synchronization primitive to use as Barriers are not there in Python2.7 import threading import time from threading import Thread,Barrier b = Barrier(2, timeout=50) def func1(): time.sleep(3) b.wait() print('Working from func1') return def func2(): time.sleep(5) b.wait() print('Working from func2') return if __name__ == '__main__': Thread(target = func1).start()

Realm data sync not consistent

╄→гoц情女王★ 提交于 2019-12-02 05:58:08
I am having an issue where Realm sometimes returns me different data every time I do the same query. Currently I am using an SyncAdapter for uploading. The idea is that we are trying to implement offline mode. So when the User creates an item it get's added to Realm db. I am generating the ID for that item manually by getting the maxId and adding 1000 to it. After that I am sending the itemID to the UploadSyncAdapter where I get the itemById and send it to the backend and the backend returns me the item with the real ID. So after that I delete the old item and just insert the new item into

How to get back the task completion status in AsyncTask

最后都变了- 提交于 2019-12-02 05:39:17
问题 This is related to my previous post Problem with downloading multiple files using AsyncTask I'm trying to download two video files and also show a ProgressDialog during the process. For this I'm using AsyncTask. I want the 1st download to complete, free up memory then start the 2nd download. I wrote the following code to achieve this, but it seems the 2nd download never begins. startDownload() { DownloadFileAsync d1 = new DownloadFileAsync(); d1.execute(videoPath+fileNames[0],fileNames[0]);

Alternative to Lock.tryLock() in Java 1.4

孤街醉人 提交于 2019-12-02 05:04:40
问题 I would like to know if there is an existing alternative or how to implement the semantics of java.util.concurrent.locks.Lock#tryLock() before Java 5. That is the possibility to back off immediately if the lock is already held by another thread. 回答1: If you need a Lock supporting a tryLock operation, you can’t use the intrinsic locking facility of Java. You have to implement your own Lock class which maintains the required state, i.e. an owner Thread and a counter and might use the intrinsic

How to make sure synchronization using the Microsoft Sync Framework was successful?

夙愿已清 提交于 2019-12-02 05:04:11
问题 I am using the Microsoft Sync Framework to synchronize a table on two Microsoft SQL Servers. I have created a test application which generates one row per second in the table on the remote server. The application making use of the Sync Framework runs on the local server. The test application created about 52000 entries in the database over one night. The syncing application executed a call to the SyncOrchestrator.Synchronize Method every 15 seconds. When I checked the outcome of the