synchronization

Why do I need to synchronize a list returned by Collections.synchronizedList

心不动则不痛 提交于 2019-11-26 20:15:14
问题 i found this at dos.oracle.com public static List synchronizedList(List list) Returns a synchronized (thread-safe) list backed by the specified list. In order to guarantee serial access, it is critical that all access to the backing list is accomplished through the returned list. It is imperative that the user manually synchronize on the returned list when iterating over it: List list = Collections.synchronizedList(new ArrayList()); ... synchronized(list) { Iterator i = list.iterator(); //

Running code on the main thread from a secondary thread?

£可爱£侵袭症+ 提交于 2019-11-26 20:14:42
问题 This is a general Java question and not an Android one first off! I'd like to know how to run code on the main thread, from the context of a secondary thread. For example: new Thread(new Runnable() { public void run() { //work out pi to 1,000 DP (takes a while!) //print the result on the main thread } }).start(); That sort of thing - I realise my example is a little poor since in Java you don't need to be in the main thread to print something out, and that Swing has an event queue also - but

Does lock() guarantee acquired in order requested?

谁说我不能喝 提交于 2019-11-26 20:13:23
When multiple threads request a lock on the same object, does the CLR guarantee that the locks will be acquired in the order they were requested? I wrote up a test to see if this was true, and it seems to indicate yes, but I'm not sure if this is definitive. class LockSequence { private static readonly object _lock = new object(); private static DateTime _dueTime; public static void Test() { var states = new List<State>(); _dueTime = DateTime.Now.AddSeconds(5); for (int i = 0; i < 10; i++) { var state = new State {Index = i}; ThreadPool.QueueUserWorkItem(Go, state); states.Add(state); Thread

std::mutex performance compared to win32 CRITICAL_SECTION

僤鯓⒐⒋嵵緔 提交于 2019-11-26 19:49:57
问题 how does the performance of std::mutex compared to CRITICAL_SECTION ? is it on par? I need lightweight synchronization object (doesn't need to be an interprocess object) is there any STL class that close to CRITICAL_SECTION other than std::mutex ? 回答1: Please see my updates at the end of the answer, the situation has dramatically changed since Visual Studio 2015. The original answer is below. I made a very simple test and according to my measurements the std::mutex is around 50-70x slower

How do determine if an object is locked (synchronized) so not to block in Java?

别来无恙 提交于 2019-11-26 19:48:32
I have a process A that contains a table in memory with a set of records (recordA, recordB, etc...) Now, this process can launch many threads that affect the records, and sometimes we can have 2 threads trying to access the same record - this situation must be denied. Specifically if a record is LOCKED by one thread I want the other thread to abort (I do not want to BLOCK or WAIT). Currently I do something like this: synchronized(record) { performOperation(record); } But this is causing me problems ... because while Process1 is performing the operation, if Process2 comes in it blocks/waits on

Volatile piggyback. Is this enough for visiblity?

不问归期 提交于 2019-11-26 19:45:02
问题 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();

How to add programmatically a custom account in android?

為{幸葍}努か 提交于 2019-11-26 19:44:03
I am trying to create an account for my app, where I will be able to have my contacts against my account like facebook, viber, whatsapp etc. I want my account to be visible in the account section of the settings also. Any ideas? I have googled a lot, but couldn't find a right answer where to start. Please help. What I have tried to create an account is as below. Which leads me to an error. Account account = new Account("Title", "com.package.nom"); String password = "password"; AccountManager accountManager = (AccountManager) MainPanel.this.getSystemService( ACCOUNT_SERVICE); accountManager

More iCloud Core Data synching woes

感情迁移 提交于 2019-11-26 19:17:04
问题 So, it finally happened. The worst case scenario for any independent iPhone developer occurred. Several users are reporting complete data loss after upgrading my app. iCloud Core Data sync is not working. My users are using this app partially to run their businesses. This is a truly catastrophic failure . The only iCloud related thing I changed was to add the key-value store to iCloud. The core data code remained exactly the same, same model version (no migration) etc. In my tests everything

Lock (Monitor) internal implementation in .NET

左心房为你撑大大i 提交于 2019-11-26 18:53:31
For mastering of some technology you have to know how it's made at one abstraction level lower. In case of multithreading programming, it will be good to know about synchronization primitives. Here is the question, how implemented Lock (Monitor) in .NET? I'm intrested in such points: - does it utilize OS objects?; - does it require user mode or kernel mode?; - what is overhead for threads that are waiting for lock?; - in what cases threads queue that awaiting for the lock could be violated?. Updated: "If more than one thread contends the lock, they are queued on a “ready queue” and granted the

How to use the CancellationToken property?

妖精的绣舞 提交于 2019-11-26 18:43:35
Compared to the preceding code for class RulyCanceler , I wanted to run code using CancellationTokenSource . How do I use it as mentioned in Cancellation Tokens , i.e. without throwing/catching an exception? Can I use the IsCancellationRequested property? I attempted to use it like this: cancelToken.ThrowIfCancellationRequested(); and try { new Thread(() => Work(cancelSource.Token)).Start(); } catch (OperationCanceledException) { Console.WriteLine("Canceled!"); } but this gave a run-time error on cancelToken.ThrowIfCancellationRequested(); in method Work(CancellationToken cancelToken) : System