If I synchronized two methods on the same class, can they run simultaneously?

If I synchronized two methods on the same class, can they run simultaneously on the same object? For example: class A { public synchronized void methodA() { //method A } public synchronized void methodB() { // method B } } I know that I can’t run methodA() twice on same object in two different threads. … Read more

What is the reason why “synchronized” is not allowed in Java 8 interface methods?

In Java 8, I can easily write: interface Interface1 { default void method1() { synchronized (this) { // Something } } static void method2() { synchronized (Interface1.class) { // Something } } } I will get the full synchronisation semantics that I can use also in classes. I cannot, however, use the synchronized modifier on … Read more

Java synchronized method lock on object, or method?

If I have 2 synchronized methods in the same class, but each accessing different variables, can 2 threads access those 2 methods at the same time? Does the lock occur on the object, or does it get as specific as the variables inside the synchronized method? Example: class X { private int a; private int … Read more

Difference between volatile and synchronized in Java

I am wondering at the difference between declaring a variable as volatile and always accessing the variable in a synchronized(this) block in Java? According to this article http://www.javamex.com/tutorials/synchronization_volatile.shtml there is a lot to be said and there are many differences but also some similarities. I am particularly interested in this piece of info: … access … Read more

What does ‘synchronized’ mean?

I have some questions regarding the usage and significance of the synchronized keyword. What is the significance of the synchronized keyword? When should methods be synchronized? What does it mean programmatically and logically? 17 s 17 The synchronized keyword is all about different threads reading and writing to the same variables, objects and resources. This … Read more