• Post Reply Bookmark Topic Watch Topic
  • New Topic

Thread doubts

 
Ravi Ramnath
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hello,
i've just now started studying threads.i hav problem with synchronization & sleep method.
what does mean ' a thread holds the lock of the object '(in synchronization)
does it mean that no other thread can enter the sync. method unless 1st one leaves the lock.
i hav also read that, sleep method doesnt relinquish the lock the thread might have.

so i've tried the program to get o/p as follows:

one:0//with sleep(200) between each line of print
one:1//
..
..
one:5
two:0
two:1
..
..
two:5

but i didnt get this by following program ,plz tell me corrections.

class MyThread extends Thread{
MyThread(String a){
super(a);
start();
}
public void run(){
SS();
}
synchronized void SS(){
for(int i=0;i<6;i++){
System.out.println(getName()+" : "+i);
try{sleep(500);}
catch(Exception e){System.out.println(getName()+":interrupted!");}
}
}
}
class ThreadDemo{
public ThreadDemo(){
MyThread t1 = new MyThread("one");
MyThread t2 = new MyThread("two");
}
public static void main(String asd[]){
new ThreadDemo();
}
}
-------------
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Moving to Threads and Synchronization...
 
Henry Wong
author
Sheriff
Posts: 22526
109
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

what does mean ' a thread holds the lock of the object '(in synchronization) does it mean that no other thread can enter the sync. method unless 1st one leaves the lock.


Yes.

but i didnt get this by following program ,plz tell me corrections.


The reason it is not working is because both threads, are using the thread object, that represents it. They are both different objects, and hence, different synchronization locks.

Henry
 
Ravi Ramnath
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi henry,

thanks for ur reply.
would you please suggest me corrections in the coding.

thanks.
ravindra h.
 
Timmy Marks
Ranch Hand
Posts: 226
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
First of all, I notice in this code:



that you are basically expecting an InterruptedException. Why, then, are you catching Exception ??


A way to get the behavior you expect would be to declare a dummy "lock" Object and pass it to the threads:



Then you would only have to change your SS() method into a block that synchronizes on lock.

 
Timmy Marks
Ranch Hand
Posts: 226
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
quote:
--------------------------------------------------------------------------------

what does mean ' a thread holds the lock of the object '(in synchronization) does it mean that no other thread can enter the sync. method unless 1st one leaves the lock.

--------------------------------------------------------------------------------



Yes.


Not exactly. The lock prevents all other threads from entering not only "the sync. method", but any method that is synchronized on the same lock. It's just a small question of semantics, but I thought it worth pointing out.
 
Henry Wong
author
Sheriff
Posts: 22526
109
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by ravindra harige:
hi henry,

thanks for ur reply.
would you please suggest me corrections in the coding.

thanks.
ravindra h.


The suggestion provided by Timmy would work perfectly. Basically, the key is to use the same synchronization lock. Using a common shared data object to lock on, using a common object that executes the code, or creating an object, just for synchronization, would work fine.

Henry
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!