• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Thread's question..

 
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
I know that when sleep() is executed it does not release the lock.But in under written code this is not the case.
Anyone who can explain it.
-----------------------------------------------
class Text{
public void writeText(){
System.out.println("Text written by Producer");
}
public void readText(){
System.out.println("Text read by Consumer");
}
}
class Producer extends Thread{
Text tx;
Producer( Text tx ){
this.tx=tx;
start();
}
public void run(){
synchronized(tx){
for(int i=0;i<5;i++){
tx.writeText();
try{
sleep(10*1000);
}catch(InterruptedException e){System.out.println("producer interrupted");}
}
}
}
}
class Consumer extends Thread {
Text tx;
Consumer( Text tx ){
this.tx=tx;
start();
}
public void run(){
synchronized(tx){
for(int i=0;i<5;i++){
tx.readText();
try{
sleep(10*1000);
}catch(Exception e){System.out.println("interrupted");}
}
}
}
}

Thanx in @dvance.
Annie.
 
Ranch Hand
Posts: 158
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well, in the example you've given, the locks are aquired on different objects. That is different instances of the Text-class. So there is no lock that will interfear with the running of the other thread.
ok?
/Mike
 
Ranch Hand
Posts: 234
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
there are two different objects running
so they will run independent of each other
go to maha anna's site and see her thread explanations
this will clear ur doubts
 
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
I think, from the code that Annie provided, you can't say whether it will be one Text object or not, it depends on how you are going to execute the two Threads(Producer and Consumer).
For example:
class Test {
public static void main (String[] args) {
new Producer(new Text());
new Consumer(new Text());
}
}
Output:
Text written by Producer
Text read by Consumer
Text written by Producer
Text read by Consumer
Text written by Producer
Text read by Consumer
Text written by Producer
Text read by Consumer
Text written by Producer
Text read by Consumer
class Test {
public static void main (String[] args) {
Text t=new Text();
new Producer(t);
new Consumer(t);
}
}
Output:
Text written by Producer
Text written by Producer
Text written by Producer
Text written by Producer
Text written by Producer
Text read by Consumer
Text read by Consumer
Text read by Consumer
Text read by Consumer
Text read by Consumer

[This message has been edited by Bin Wang (edited June 13, 2001).]
 
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Bin,
Will you please explain the reason for different output you are getting for your code above.
Basically you are giving the same object.
Avinash
 
Ranch Hand
Posts: 195
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Bin's 2 code samples have a very important distinction between them. In the first case

the argument to the Producer and Consumer threads are separate instances of a Text object.
In the second example,

the argument to the Producer and Consumer threads is the same instance of a Text object.
Since each object has its own lock, both Producer and Consumer threads are able to simultaneously execute the synchronized code referencing the Test objects in the first example.
In the second example, however, there is only a single Text object and it can only give its lock to a single thread at a time. Therefore, the second thread to try to run the synchronized code must wait until the first thread finishes the method so that it may obtain the lock.
 
Avinash Rai
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanx for the explaination Scott.
Avinash
 
Annie Naqvi
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
Thanx to all for help.Scott thanx for your elaborate explaination. I was missing the very obvious point regarding synchronization and Bin has provided that. I got it.
Thanx again.
Annie.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic