synchronization

Volatile piggyback. Is this enough for visiblity?

不问归期 提交于 2019-11-27 12:07:14
This is about volatile piggyback. Purpose: I want to reach a lightweight vars visibilty. Consistency of a_b_c is not important. I have a bunch of vars and I don't want to make them all volatile. Is this code threadsafe? class A { public int a, b, c; volatile int sync; public void setup() { a = 2; b = 3; c = 4; } public void sync() { sync++; } } final static A aaa = new A(); Thread0: aaa.setup(); end Thread1: for(;;) {aaa.sync(); logic with aaa.a, aaa.b, aaa.c} Thread2: for(;;) {aaa.sync(); logic with aaa.a, aaa.b, aaa.c} Adam Zalcman Java Memory Model defines the happens-before relationship

Android: How to get a modal dialog or similar modal behavior?

最后都变了- 提交于 2019-11-27 12:00:47
These days I'm working on simulating modal dialog in Android. I've googled a lot, there's much discussions but sadly there's not much options to get it modal. Here's some background, Dialogs, Modal Dialogs and Blockin Dialogs / AlertDialogs: How to "block execution" while dialog is up (.NET-style) There's no straight way to get modal behavior, then I came up with 3 possible solutions, 1. Use a dialog-themed activity, like this thread said, but I still can't make main activity truly wait for dialog-activity return. Main activity turned to stop status and got restarted then. 2. Build one worker

Lock free stack and queue in C#

自古美人都是妖i 提交于 2019-11-27 12:00:15
问题 Does anyone know if there are any lock-free container libraries available for .NET ? Preferably something that is proven to work and faster than the Synchronized wrappers we have in .NET. I have found some articles on the .NET, but none of them specify any speed benchmarking, nor do they inspire much confidence in their reliability. Thanks 回答1: Late, but better than never I thought I would add Julian Bucknalls articles to this list. But he does not have performance numbers. In my testing of

What happens if two process in different processors try to acquire the lock at EXACTLY same time

我们两清 提交于 2019-11-27 11:20:34
问题 Ok, so I am reading about synchronization, and I read through various algorithms such as spinlocks, semaphores, and mutex to avoid race condition. However, these algorithms can't prevent race condition in SMP when multiple proceses access the data exactly at the same time. For example, suppose thread 1 in processor A runs lock(mutex1); withdraw(1000); unlock(mutex1); and thread 2 in processor B runs lock(mutex1); deposit(1000); deposit(1000); unlock(mutex1); When both threads run EXACTLY AT

What Cases Require Synchronized Method Access in Java?

守給你的承諾、 提交于 2019-11-27 11:16:54
问题 In what cases is it necessary to synchronize access to instance members? I understand that access to static members of a class always needs to be synchronized- because they are shared across all object instances of the class. My question is when would I be incorrect if I do not synchronize instance members? for example if my class is public class MyClass { private int instanceVar = 0; public setInstanceVar() { instanceVar++; } public getInstanceVar() { return instanceVar; } } in what cases

Atomic Operations and multithreading

China☆狼群 提交于 2019-11-27 10:49:53
问题 Recently I was reading a tutorial, in that I came across a statement that says.. "The Java language specification guarantees that reading or writing a variable is an atomic operation(unless the variable is of type long or double ). Operations variables of type long or double are only atomic if they declared with the volatile keyword." AtomicInteger or AtomicLong that provides methods like getAndDecrement() , getAndIncrement() and getAndSet() which are atomic. I got confused a bit with the

SQL Server - synchronizing 2 tables on 2 different databases

怎甘沉沦 提交于 2019-11-27 10:37:48
问题 I have 2 tables with same schema on 2 different databases on the same server with SQL Server 2008 R2. One table gets updated with data more often. Now there is a need to keep these 2 table in sync. This can happen as a nightly process. What is the best methodology to achieve the sync. process ? 回答1: Using MERGE is your best bet. You can control each of the conditions. WHEN MATCHED THEN, WHEN UNMATCHED THEN etc. MERGE - Technet MERGE- MSDN (GOOD!) Example A: Transactional usage - Table

Symbolic links and synced folders in Vagrant

柔情痞子 提交于 2019-11-27 10:05:51
I want to use Vagrant to provide a common development environment to my team. The hosts are completely different: Some use OS X, some Linux, and some Windows. Some use VMware, some use VirtualBox. Inside of the VM we want to run Linux. So far, everything is fine. Now our idea was that each developer shall be able use the IDE of their choice, and hence we have introduced a synced folder that shares the source code between the host and the VM. This basically, works as well … except for symbolic links. Inside of our source code we actually do have a few symbolic links, which is not a problem

ConcurrentHashMap: avoid extra object creation with “putIfAbsent”?

让人想犯罪 __ 提交于 2019-11-27 09:45:33
问题 I am aggregating multiple values for keys in a multi-threaded environment. The keys are not known in advance. I thought I would do something like this: class Aggregator { protected ConcurrentHashMap<String, List<String>> entries = new ConcurrentHashMap<String, List<String>>(); public Aggregator() {} public void record(String key, String value) { List<String> newList = Collections.synchronizedList(new ArrayList<String>()); List<String> existingList = entries.putIfAbsent(key, newList); List

synchronized block for an Integer object

China☆狼群 提交于 2019-11-27 09:36:23
I just came across the synchronized block in Java and wrote a small programm to test how it works. I create 10 threads and let each thread increment an Integer object 1000 times. So with synchronization I would assume a result of 10000 after all threads have finished their work and a result of less than 10000 without synchronization . However the synchronization is not wokring as I expected. I guess it has something to do with immutability of the object or so. My program: public class SyncTest extends Thread{ private static Integer syncObj = new Integer(0); private static SyncTest[] threads =