• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

Thread doubts

 
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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();
}
}
-------------
 
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Moving to Threads and Synchronization...
 
author
Posts: 23958
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi henry,

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

thanks.
ravindra h.
 
Ranch Hand
Posts: 226
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 23958
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
We should throw him a surprise party. It will cheer him up. We can use this tiny ad:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic