java - Thread locking in synchronized method -


i read every object in java have lock.that acquired thread when synchronized method called object.and @ same time thread can acquire more 1 lock.

public class threadtes extends thread{  public static void main(string[] args){      threadtes t=new threadtes();     t.start();  } synchronized public void r(){     system.out.println("in r method");     this.r1(); } synchronized public void r1(){     system.out.println("in r1 method"); } public void run(){     threadtes tt=new threadtes();     tt.r();         } } 

in above code 1 synchronized method r() called run , particular thread acquire lock of object tt.now again r() r1() called.

my question tt lock acquired enters r() how come enters r1() lock on object acquired.

public class threadtes extends thread{  public static void main(string[] args){      threadtes t=new threadtes();     t.start();  } synchronized public void r(){     system.out.println("in r method");     threadtes ttt=new threadtes();     ttt.r1(); } synchronized public void r1(){     system.out.println("in r1 method"); } public void run(){     threadtes tt=new threadtes();     tt.r();     } } 

how come code running running 2 different objects...

java's intrinsic locks (used synchronized keyword) re-entrant. lock required enter r1 same lock 1 required enter r. thread owns lock allowed re-acquire it. can think of there being counter number of times thread has acquired same lock. in method r1, calling thread has acquired same lock twice. lock available other threads once "count" goes zero; is, once thread obtained lock has released many times obtained it.

edit: here example of 5 threads each bounce between 2 mutually-recursive methods both require same lock. running example can see no thread enter methodone until thread before has finished recursing, , there no problem same thread continually enter same (or different) synchronized methods share same lock.

public class lockexample {     private static synchronized void methodone(int depth) {         if (depth == 0) {             return;         } else {             system.out.println(thread.currentthread().getname() + " methodone (in), depth " + depth);             methodtwo(depth - 1);             system.out.println(thread.currentthread().getname() + " methodone (out), depth " + depth);         }     }      private static synchronized void methodtwo(int depth) {         if (depth == 0) {             return;         } else {             system.out.println(thread.currentthread().getname() + " methodtwo (in), depth " + depth);             methodone(depth - 1);             system.out.println(thread.currentthread().getname() + " methodtwo (out), depth " + depth);         }     }      public static void main(string[] args) {         (int = 0; < 5; i++) {             thread t = new thread(new runnable() {                 @override                 public void run() {                     methodone(10);                 }             });             t.setname("thread" + i);             t.start();         }     } } 

sample output (sorry length):

thread0 methodone (in), depth 10 thread0 methodtwo (in), depth 9 thread0 methodone (in), depth 8 thread0 methodtwo (in), depth 7 thread0 methodone (in), depth 6 thread0 methodtwo (in), depth 5 thread0 methodone (in), depth 4 thread0 methodtwo (in), depth 3 thread0 methodone (in), depth 2 thread0 methodtwo (in), depth 1 thread0 methodtwo (out), depth 1 thread0 methodone (out), depth 2 thread0 methodtwo (out), depth 3 thread0 methodone (out), depth 4 thread0 methodtwo (out), depth 5 thread0 methodone (out), depth 6 thread0 methodtwo (out), depth 7 thread0 methodone (out), depth 8 thread0 methodtwo (out), depth 9 thread0 methodone (out), depth 10 thread2 methodone (in), depth 10 thread2 methodtwo (in), depth 9 thread2 methodone (in), depth 8 thread2 methodtwo (in), depth 7 thread2 methodone (in), depth 6 thread2 methodtwo (in), depth 5 thread2 methodone (in), depth 4 thread2 methodtwo (in), depth 3 thread2 methodone (in), depth 2 thread2 methodtwo (in), depth 1 thread2 methodtwo (out), depth 1 thread2 methodone (out), depth 2 thread2 methodtwo (out), depth 3 thread2 methodone (out), depth 4 thread2 methodtwo (out), depth 5 thread2 methodone (out), depth 6 thread2 methodtwo (out), depth 7 thread2 methodone (out), depth 8 thread2 methodtwo (out), depth 9 thread2 methodone (out), depth 10 thread3 methodone (in), depth 10 thread3 methodtwo (in), depth 9 thread3 methodone (in), depth 8 thread3 methodtwo (in), depth 7 thread3 methodone (in), depth 6 thread3 methodtwo (in), depth 5 thread3 methodone (in), depth 4 thread3 methodtwo (in), depth 3 thread3 methodone (in), depth 2 thread3 methodtwo (in), depth 1 thread3 methodtwo (out), depth 1 thread3 methodone (out), depth 2 thread3 methodtwo (out), depth 3 thread3 methodone (out), depth 4 thread3 methodtwo (out), depth 5 thread3 methodone (out), depth 6 thread3 methodtwo (out), depth 7 thread3 methodone (out), depth 8 thread3 methodtwo (out), depth 9 thread3 methodone (out), depth 10 thread1 methodone (in), depth 10 thread1 methodtwo (in), depth 9 thread1 methodone (in), depth 8 thread1 methodtwo (in), depth 7 thread1 methodone (in), depth 6 thread1 methodtwo (in), depth 5 thread1 methodone (in), depth 4 thread1 methodtwo (in), depth 3 thread1 methodone (in), depth 2 thread1 methodtwo (in), depth 1 thread1 methodtwo (out), depth 1 thread1 methodone (out), depth 2 thread1 methodtwo (out), depth 3 thread1 methodone (out), depth 4 thread1 methodtwo (out), depth 5 thread1 methodone (out), depth 6 thread1 methodtwo (out), depth 7 thread1 methodone (out), depth 8 thread1 methodtwo (out), depth 9 thread1 methodone (out), depth 10 thread4 methodone (in), depth 10 thread4 methodtwo (in), depth 9 thread4 methodone (in), depth 8 thread4 methodtwo (in), depth 7 thread4 methodone (in), depth 6 thread4 methodtwo (in), depth 5 thread4 methodone (in), depth 4 thread4 methodtwo (in), depth 3 thread4 methodone (in), depth 2 thread4 methodtwo (in), depth 1 thread4 methodtwo (out), depth 1 thread4 methodone (out), depth 2 thread4 methodtwo (out), depth 3 thread4 methodone (out), depth 4 thread4 methodtwo (out), depth 5 thread4 methodone (out), depth 6 thread4 methodtwo (out), depth 7 thread4 methodone (out), depth 8 thread4 methodtwo (out), depth 9 thread4 methodone (out), depth 10 

Comments

Popular posts from this blog

php - What is the difference between $_SERVER['PATH_INFO'] and $_SERVER['ORIG_PATH_INFO']? -

fortran - Function return type mismatch -

queue - mq_receive: message too long -