java - Instance Synchronization -


i wrote small block of code understand concepts of synchronized blocks:

public class objectlevelsynchronized {      public void run() {         synchronized(this) {             try {                 system.out.println(thread.currentthread().getname());                 thread.sleep(100);                 system.out.println(thread.currentthread().getname() + " finished.");             }catch(exception e) {              }         }     }      public static void main(string[] args) throws exception {          final objectlevelsynchronized c = new objectlevelsynchronized();         final objectlevelsynchronized c1 = new objectlevelsynchronized();                 thread t = new thread(new runnable() {             public void run() {                 c.run();                 c.run();             }         }, "mythread1");          thread t1 = new thread(new runnable() {             public void run() {                 c1.run();                 c1.run();             }         }, "mythread2");          t.start();         t1.start();     } } 

only 1 thread can execute inside java code block synchronized on same monitor object, calling run method 2 different threads , 2 different instances.

expected result:

mythread1 mythread2 mythread1 finished mythread2 finished mythread1 mythread2  mythread1 finished mythread2 finished 

actual result

mythread1 mythread1 finished. mythread1 mythread1 finished. mythread2 mythread2 finished. mythread2 mythread2 finished. 

why synchronized block, locked code 2 different objects?

you have 2 different lock.

you use synchronized(this)

and create 2 objects

final classlevelsynchronized c = new classlevelsynchronized(); final classlevelsynchronized c1 = new classlevelsynchronized();      

so, c , c1 locks , synchronized looks these locks. have use same object synchronize block. simplest way

private static object lock = new object(); 

and use in synchronized

synchronized(lock) 

or, output, should work too.

public static void main(string[] args) throws exception {          final objectlevelsynchronized c = new objectlevelsynchronized();         final objectlevelsynchronized c1 = new objectlevelsynchronized();                final object lock = new object();         thread t = new thread(new runnable() {             public void run() {                 synchronized (lock) {                     c.run();                     c.run();                 }             }         }, "mythread1");          thread t1 = new thread(new runnable() {             public void run() {                 synchronized (lock) {                     c1.run();                     c1.run();                 }             }         }, "mythread2");          t.start();         t1.start();     } 

Comments

Popular posts from this blog

matlab - "Contour not rendered for non-finite ZData" -

delphi - Indy UDP Read Contents of Adata -

javascript - Any ideas when Firefox is likely to implement lengthAdjust and textLength? -