Help coderanch get a
new server
by contributing to the fundraiser
  • 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
  • Ron McLeod
  • Paul Clapham
  • Devaka Cooray
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Tim Moores
  • Carey Brown
  • Mikalai Zaikin
Bartenders:
  • Lou Hamers
  • Piet Souris
  • Frits Walraven

sleep()

 
Ranch Hand
Posts: 153
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi! When i run the following code, the output is:
entered in run
entered in run
end of run
end of run
As the run() method is synchronized and the sleep() method doesn't release the lock so the second thread shouldn't get a chance to enter in run(). But her as the output shows on calling the sleep() the second thread enters in the run method and executes //1. Why is it so? Can u help me pl?
thanks.
ashok.
______
class TestThread extends Thread {
public synchronized void run() {
System.out.println("entered in run"); //1
try {
sleep(1000);
} catch(InterruptedException ie) {}
System.out.println("end of run"); //2
}
public static void main(String args[]) {
new TestThread().start();
new TestThread().start();
}
}
________
 
Author & Gold Digger
Posts: 7617
6
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Because you create 2 different Thread instances. So declaring run as synchronized won't give you the expected behaviour,since each of the Thread object will be able to get the lock on themselves without having to care about the other thread. What you could do is a class lock on the Thread class, and it should work, let me know !
HIH
Valentin
 
mister krabs
Posts: 13974
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That's correct. The run() method is synchronized on the object.
 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
that's right. you created 2 instance of TestThread, each has a instance method run() belong to different object.
if you change the code to
class TestThread extends Thread {
public void run() {
run_test();
}
public static synchronized void run_test() {
System.out.println("entered in run"); //1
try {
sleep(1000);
} catch(InterruptedException ie) {}
System.out.println("end of run"); //2
}
public static void main(String args[]) {
new TestThread().start();
new TestThread().start();
}
}
then the resule will be
entered in run
end of run
entered in run
end of run
this is because the method run_test() only have one entity, its belong to the class.
 
ashok khetan
Ranch Hand
Posts: 153
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks a lot galen,valentin,Thomas!!
I got the point!!
ashok.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic