synchronization

What should I use as a lock object of a synchronized statement in Java

拟墨画扇 提交于 2019-12-05 13:12:12
Could anyone explain what is the difference between these examples? Example # 1. public class Main { private Object lock = new Object(); private MyClass myClass = new MyClass(); public void testMethod() { // TODO Auto-generated method stub synchronized (myClass) { // TODO: modify myClass variable } } } Example # 2. package com.test; public class Main { private MyClass myClass = new MyClass(); private Object lock = new Object(); public void testMethod() { // TODO Auto-generated method stub synchronized (lock) { // TODO: modify myClass variable } } } What should I use as a monitor lock if I need

Is entering synchronized block atomic?

≯℡__Kan透↙ 提交于 2019-12-05 12:39:35
Do you know if there is guaranteed that synchronized block in java is atomic? Imagine following case Thread_1,2: synchronized(object){object.modify();} (object is shared variable.) imagine thread_M will change reference to object like synchronized(object){object = new Object()} now imagine threads 1 and 2 are competing over getting the lock on object Is it possible that following would happened: 1. Thread1: read old object 2. ThreadM: modify object reference & release old object lock 3. Thread2: read new object; check lock; lock on it 4. Thread1: check lock (ok cos old object was read); lock

Does writing the same value to the same memory location cause a data race?

我怕爱的太早我们不能终老 提交于 2019-12-05 12:37:10
问题 Consider the following code that writes the same value to the same memory location from multiple threads: void f(int* buf, int n, int* p) { for(int i = 0; i < n; i++) buf[i] = i; *p = buf[n/2]; } void g(int* buf, int n) { int x1, x2; thread t1(f, buf, n, &x1); thread t2(f, buf, n, &x2); t1.join(); t2.join(); assert(x1 == x2); } Although it's interesting, I'm of less concern of what guarantees the standard gives, since I guess it gives none. What I do care is what will be the behavior of the

should I *always* synchronize access to all double field/property/variables that used from more than one thread?

前提是你 提交于 2019-12-05 12:21:49
Note I tend to write lock-free code so I try to avoid any types of lock where possible. Instead I just use while(true) loop because I have a lot of CPU power. According to that http://msdn.microsoft.com/en-us/library/aa691278%28VS.71%29.aspx double variable update is not atomic. I'm concerned about two issues: if one thread modify variable of field or property and another thread read it at the same time i want to have either previous or new value, but I don't want to receive something strange. I.e. if one thread changes value from 5.5 to 15.15 I want to have in another thread one of these two

Named Lock Collection in C#?

落爺英雄遲暮 提交于 2019-12-05 12:02:27
I have multiple threads writing data to a common source, and I would like two threads to block each other if and only if they are touching the same piece of data. It would be nice to have a way to lock specifically on an arbitrary key: string id = GetNextId(); AquireLock(id); try { DoDangerousThing(); } finally { ReleaseLock(id); } If nobody else is trying to lock the same key, I would expect they would be able to run concurrently. I could achieve this with a simple dictionary of mutexes, but I would need to worry about evicting old, unused locks and that could become a problem if the set

Strategies for keeping two mySQL databases (in separate locations) in sync? [duplicate]

柔情痞子 提交于 2019-12-05 11:52:43
This question already has answers here : Closed 7 years ago . Possible Duplicate: Which is the best way to bi-directionally synchronize dynamic data in real time using mysql I need advice: How do I keep 2 mySQL databases (same structure) synced up across 2 locations? What am I doing: I am creating an application for a business with two locations. They need to share/update information in a database BUT the proprietor wants it to be internal not external, as he is worried about being able to run in case of an internet outage. My Strategy so far: I've set up two servers (one in each location)

Reliable background synchronisation on iOS

て烟熏妆下的殇ゞ 提交于 2019-12-05 11:24:19
Preamble I wrote a mobile application which should show upcoming events. The app downloads it's data from server. Data is prepared in batches once every 24 hours and it's ready to be fetched after 4 am. This gives me a perfect opportunity to sync it overnight and to make new data immediately available when the user opens the app. Background fetch This was my first approach for syncing data with the server. It's advertised as very powerful feature, but using it alone (out of the test environment) is not enough: There is no way to force background fetches to execute at exact intervals. I thought

Syncing Drupal site between dev, staging and production

守給你的承諾、 提交于 2019-12-05 11:15:48
Often after a Drupal (6.x) site is launched, I have people starting to sign up and enter their own content. Whenever there is need for an upgrade, the database on production is copied to dev and then the development is done on dev, later get pushed to staging for client's approval. When the site is eventually ready to go live, there is a problem. Production server has the latest user inputted content, dev and staging have the latest functionality. Simply overwriting the database on production won't work. What I usually do is to write down what has been done to dev and than follow the steps to

Block level synchronization

拈花ヽ惹草 提交于 2019-12-05 10:33:30
What is the significance of parameter passed to synchronized? synchronized ( parameter ) { } to achieve block level synchronization. Somewhere i saw code like class test { public static final int lock =1; ... synchronized(lock){ ... } } I don't understand the purpose of this code. Can anyone give me a better example and/or explain it? It's the reference to lock on. Basically two threads won't execute blocks of code synchronized using the same reference at the same time. As Cletus says, a synchronized method is mostly equivalent to using synchronized (this) inside the method. I very much hope

Is this broken double checked locking?

前提是你 提交于 2019-12-05 09:56:17
Checkstyle reports this code as "The double-checked locking idiom is broken", but I don't think that my code actually is affected by the problems with double-checked locking. The code is supposed to create a row in a database if a row with that id doesn't exist. It runs in a multi-threaded environment and I want to avoid the primary-key-exists SQL-exceptions. The pseudo-code: private void createRow(int id) { Row row = dao().fetch(id); if (row == null) { synchronized (TestClass.class) { row = dao().fetch(id); if (row == null) { dao().create(id); } } } } I can agree that it looks like double