synchronization

Double checked locking Article

痞子三分冷 提交于 2019-11-27 02:46:35
问题 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

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

我与影子孤独终老i 提交于 2019-11-27 02:28:22
问题 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

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

瘦欲@ 提交于 2019-11-27 02:21:02
问题 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

Explain synchronization of collections when iterators are used?

不问归期 提交于 2019-11-27 01:34:33
问题 I understand that collections like the Hashtable are synchronized, but can someone explain to me how it works, and at what point(s) access is restricted to concurrent calls? For example, let's say I use some iterators like this: Hashtable<Integer,Integer> map = new Hashtable<Integer,Integer>(); void dosomething1(){ for (Iterator<Map.Entry<Integer,Integer>> i = map.entrySet().iterator(); i.hasNext();){ // do something } } void dosomething2(){ for (Iterator<Map.Entry<Integer,Integer>> i = map

Why does notifyAll() raise IllegalMonitorStateException when synchronized on Integer?

房东的猫 提交于 2019-11-27 01:01:51
问题 Why does this test program result in a java.lang.IllegalMonitorStateException ? public class test { static Integer foo = new Integer(1); public static void main(String[] args) { synchronized(foo) { foo++; foo.notifyAll(); } System.err.println("Success"); } } Result: Exception in thread "main" java.lang.IllegalMonitorStateException at java.lang.Object.notifyAll(Native Method) at test.main(test.java:6) 回答1: You have noted correctly that notifyAll must be called from a synchronized block.

Sync JS time between multiple devices

て烟熏妆下的殇ゞ 提交于 2019-11-27 00:58:15
I'm using the wonderful reveal.js library to create a HTML slideshow. My only problem is that I need it to synchronise across multiple devices. At the moment I am making a AJAX request to the the time from the server and keep an internal clock for the page. function syncTime() { // Set up our time object, synced by the HTTP DATE header // Fetch the page over JS to get just the headers console.log("syncing time") var r = new XMLHttpRequest(); r.open('HEAD', document.location, false); r.send(null); var timestring = r.getResponseHeader("DATE"); systemtime = new Date(timestring); // Set the time

Waiting on multiple threads to complete in Java

浪子不回头ぞ 提交于 2019-11-27 00:38:02
During the course of my program execution, a number of threads are started. The amount of threads varies depending on user defined settings, but they are all executing the same method with different variables. In some situations, a clean up is required mid execution, part of this is stopping all the threads, I don't want them to stop immediately though, I just set a variable that they check for that terminates them. The problem is that it can be up to 1/2 second before the thread stops. However, I need to be sure that all threads have stopped before the clean up can continues. The cleanup is

Monitor vs Mutex in c# [duplicate]

ε祈祈猫儿з 提交于 2019-11-27 00:16:55
Possible Duplicate: What are the differences between various threading synchronization options in C#? What is the difference between a Monitor and a Mutex in C#? When to use a Monitor and when to use a Mutex in C#? A Monitor is managed, and more lightweight - but is restricted to your AppDomain . A Mutex can be named, and can span processes (allowing some simple IPC scenarios between applications), and can be used in code that wants a wait-handle). For most simple scenarios, Monitor (via lock ) is fine. A good source of advice on this stuff is the "Threading in C#" by Joseph Albahari. All the

Difference between Hashtable and Collections.synchronizedMap(HashMap)

萝らか妹 提交于 2019-11-27 00:14:01
问题 As far as I know, java.util.Hashtable synchronizes each and every method in the java.util.Map interface, while Collections.synchronizedMap(hash_map) returns a wrapper object containing synchronized methods delegating calls to the actual hash_map (correct me if I am wrong). I have two questions : What difference does it make to synchronize each and every method and to have a wrapper class? What are the scenarios to choose one over the other? What happens when we do Collections.synchronizedMap

How is thread synchronization implemented, at the assembly language level?

依然范特西╮ 提交于 2019-11-27 00:11:36
问题 While I'm familiar with concurrent programming concepts such as mutexes and semaphores, I have never understood how they are implemented at the assembly language level. I imagine there being a set of memory "flags" saying: lock A is held by thread 1 lock B is held by thread 3 lock C is not held by any thread etc But how is access to these flags synchronized between threads? Something like this naive example would only create a race condition: mov edx, [myThreadId] wait: cmp [lock], 0 jne wait