synchronization

Are there any differences between Java's “synchronized” and C#'s “lock”?

房东的猫 提交于 2019-11-28 10:58:30
Do these two keywords have exactly the same effect, or is there something I should be aware of? Keeg According to this site: http://en.csharp-online.net/CSharp_FAQ:_What_is_the_difference_between_CSharp_lock_and_Java_synchronized , C# lock and Java synchronized code blocks are "semantically identical", while for methods, Java uses synchronized while C# uses an attribute: [MethodImpl(MethodImplOptions.Synchronized)] . Jon Skeet One interesting difference not covered in the link posted by Keeg: as far as I'm aware, there's no equivalent method calls in Java for .NET's Monitor.Enter and Monitor

How do I wait for a SwingWorker's doInBackground() method?

柔情痞子 提交于 2019-11-28 10:56:26
Say I have the following code: import java.lang.InterruptedException; import javax.swing.SwingWorker; public class Test { private JDialog window; public Test { // instantiate window } private class Task extends SwingWorker<Void, Void> { public Void doInBackground() { try { Thread.currentThread().sleep(5000); } catch(InterruptedException e) {} return null; } } public void doTask() { Task task = new Task(); task.execute(); } protected void process() { // update various GUI components here } public static void main(String args[]) { Test t = new Test(); t.doTask(); System.out.println("done"); } }

Synchronous query to Web SQL Database

痞子三分冷 提交于 2019-11-28 09:54:51
I'm working on a bit of JavaScript that interacts with a client-side SQLite database, via the newish window.openDatabase(...) , database.transaction(...) and related APIs. As most of you know when you execute a query in this way it is an asynchronous call, which is typically good. You can make the call and handle the results as appropriate with callbacks. In my current situation I'm working on an algo for a client that does some hierarchy walking in the locally stored database. The part of the algo I'm having trouble with requires starting at some row, which has a reference to a "parent" (by

What is a good way to test that a Java method is synchronized?

…衆ロ難τιáo~ 提交于 2019-11-28 09:48:41
I have several classes that implement some interface. The interface has a contract, that some methods should be synchronized, and some should not, and I want to verify that contract through unit tests for all the implementations. The methods should use the synchronized keyword or be locked on this - very similar to the synchronizedCollection() wrapper. That means I should be able to observe it externally. To continue the example of Collections.synchronizedCollection() if I have one thread calling iterator(), I should still be able to get into methods like add() with another thread because

condition variable [closed]

£可爱£侵袭症+ 提交于 2019-11-28 09:31:21
What are the principles of a condition variable in synchronization of the processes of operating systems? Alexander Sandler Well, conditional variables allow you to wait for certain condition to occur. In practice your thread may sleep on conditional variable and other thread wakes it up. Conditional variable also usually comes with mutex. This allows you to solve following synchronisation problem: how can you check state of some mutex protected data structure and then wait until it's state changes to something else. I.e. /* in thread 1 */ pthread_mutex_lock(mx); /* protecting state access */

Synchronizing STD cout output multi-thread

删除回忆录丶 提交于 2019-11-28 09:27:05
Latelly I've been working with multi-thread coding, after a while writing I realized that if I used std::cout in different boost::threads, the output would came without a logical order, the program that I'm testing is something like: #include <boost/thread/thread.hpp> #include <iostream> int my01( void ) { std::cout << "my01" << std::endl; return 0; } /* my02, my03 and my04 are the same with different outputs*/ [...] int main( void ) { boost::thread t1(&my01); boost::thread t2(&my02); boost::thread t3(&my03); boost::thread t4(&my04); while(!t1.joinable() || !t2.joinable() || !t3.joinable() ||

Double checked locking Article

帅比萌擦擦* 提交于 2019-11-28 09:15:56
I was reading this article about "Double-Checked locking" and out of the main topic of the article I was wondering why at some point of the article the author uses the next Idiom: Listing 7. Attempting to solve the out-of-order write problem public static Singleton getInstance() { if (instance == null) { synchronized(Singleton.class) { //1 Singleton inst = instance; //2 if (inst == null) { synchronized(Singleton.class) { //3 inst = new Singleton(); //4 } instance = inst; //5 } } } return instance; } And my question is: Is there any reason to synchronize twice some code with the same lock? Have

What's the difference between Synchronized and Lock in my example?

白昼怎懂夜的黑 提交于 2019-11-28 08:51:05
I wrote a simple code to mock concurrency using Lock and synchronized . Source code is as follows: Task class includes a method named doSomething() to print the thread name and executing elapsed time. import java.util.Calendar; public class Task { public void doSomething() { try { Thread.sleep(2000); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } StringBuilder sb = new StringBuilder(); //Thread Name sb.append("Thread Name: ").append(Thread.currentThread().getName()); //Timestamp for the executing sb.append(", elaspsed time: ").append(Calendar

How should Finder Sync Extension and Main App communicate?

泪湿孤枕 提交于 2019-11-28 08:49:14
My use case: I have a 'MainApp' which does the syncing of files. I would like that 'MainApp' handles all server calls regarding syncing and other REST API calls such as document-sharing, etc. On the other hand, I would have a Finder Sync Extension which would show sync-status icon overlays. It would also have a file-context-menu-item 'Share' which would present a Share dialog where users can choose with whom to share the file. Questions: How should FinderSyncExtension and MainApp communicate? Should XCP be utilised and if so, is it ok that communication is two-ways? For example MainApp

What is the scope of memory flushed or published to various threads when using volatile and synchronized?

依然范特西╮ 提交于 2019-11-28 08:40:39
This question is in reference to memory visibility only, not happens-before and happens-after. There are four ways in Java that guarantees changes to memory in one thread to be made visible to another thread. (reference http://gee.cs.oswego.edu/dl/cpj/jmm.html ) A writing thread releases a synchronization lock and a reading thread subsequently acquires that same synchronization lock. If a field is declared as volatile, any value written to it is flushed and made visible by the writer thread before the writer thread performs any further memory operation (i.e., for the purposes at hand it is