Let's consider such a case. We have a class, that has a field which can be both accessed from a static synchronized method and another, non-static synchronized method. Both of them are synchronized so everything should be fine. But it is not. This example illustrates our problem:

` public class MyClass implements Runnable { `

private static int a = 5;

public synchronized static void play() {

for(int i = 0; i < 10; i++) {

System.out.println(Thread.currentThread().getName() + " work with " + a + " and is counting: " + i);

try {

Thread.sleep(1);

} catch(Exception e) {}

}

}

public synchronized void play2() {

for(int i = 0; i < 10; i++) {

System.out.println(Thread.currentThread().getName() + " work with " + a + " and is counting: " + i);

}

}

public void run() {

if(Thread.currentThread().getName().equals("first")) {

play();

}

else {

play2();

}

}

public static void main(String[] args) {

MyClass myClass = new MyClass();

Thread first = new Thread(myClass);

Thread second = new Thread(myClass);

first.setName("first");

second.setName("second");

first.start();

second.start();

}

}

And the possible output that we can get is as follows (it will vary anytime you run the code):

first work with 5 and is counting: 0

first work with 5 and is counting: 1

second work with 5 and is counting: 0

second work with 5 and is counting: 1

first work with 5 and is counting: 2

second work with 5 and is counting: 2

second work with 5 and is counting: 3

second work with 5 and is counting: 4

second work with 5 and is counting: 5

first work with 5 and is counting: 3

second work with 5 and is counting: 6

second work with 5 and is counting: 7

second work with 5 and is counting: 8

first work with 5 and is counting: 4

second work with 5 and is counting: 9

first work with 5 and is counting: 5

first work with 5 and is counting: 6

first work with 5 and is counting: 7

first work with 5 and is counting: 8

first work with 5 and is counting: 9

And as I said before it's not fine. The operations of counting are interfering each other like in case of no synchronization. The problem is due to the different locks that are acquired by the non-static and static methods. The static methods acquire the lock belonging to this in contrary to the non-static methods which acquire the lock of java.lang.Class that every loaded class has.

So during your programming voyages be careful to this!

## No comments:

## Post a Comment