Synchronization vs Lock

If you’re simply locking an object, I’d prefer to use synchronized Example: Lock.acquire(); doSomethingNifty(); // Throws a NPE! Lock.release(); // Oh noes, we never release the lock! You have to explicitly do try{} finally{} everywhere. Whereas with synchronized, it’s super clear and impossible to get wrong: synchronized(myObject) { doSomethingNifty(); } That said, Locks may be … Read more

What is the difference between atomic / volatile / synchronized?

You are specifically asking about how they internally work, so here you are: No synchronization private int counter; public int getNextUniqueIndex() { return counter++; } It basically reads value from memory, increments it and puts back to memory. This works in single thread but nowadays, in the era of multi-core, multi-CPU, multi-level caches it won’t … Read more

C# version of java’s synchronized keyword?

First – most classes will never need to be thread-safe. Use YAGNI: only apply thread-safety when you know you actually are going to use it (and test it). For the method-level stuff, there is [MethodImpl]: [MethodImpl(MethodImplOptions.Synchronized)] public void SomeMethod() {/* code */} This can also be used on accessors (properties and events): private int i; … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)