• 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

Question on lock

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
pro1:
public class AA implements Runnable{
String ss=new String();
public void f(){
synchronized(ss){
try {System.out.println("hello");wait();}catch(Exception e){}
}}

public void run(){f();}

public static void main(String[] args) {
AA a =new AA();
new Thread(a,"no1").start();
new Thread(a,"no2").start();
}}

the output is :
hello
hello
and then program ends. Why end???the two are both in wait state ,I think.

but
pro2:
public class AA implements Runnable{
String ss=new String();
public void f(){
synchronized(this){
try {System.out.println("hello");wait();}catch(Exception e){}
}}

public void run(){f();}

public static void main(String[] args) {
AA a =new AA();
new Thread(a,"no1").start();
new Thread(a,"no2").start();
}}

output is
hello
hello
and program does not end.

I am wondering about the lock, what the different effect between this and other object lock.

Thank you!
[ August 12, 2004: Message edited by: Ning Zhuang ]
 
Ranch Hand
Posts: 108
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here's another topic started by Victor regarding synchronization problems.
I still have to understand tht one...and here you put another question

Threads just keep confusing me over n over !!

Synchronization--Victor Santos
 
Ranch Hand
Posts: 102
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The example in pro1 does not signify anything, as the thread which calls this.wait() gets java.lang.IllegalMonitorStateException
immediately and is terminated. A e.printStackTrace() will reveal that. The reason of the exception is explained in (�17.14 of Java Lang Specification).

In the second case, each thread locks its own object, and then calls this.wait(), which releases the lock and literally goes in
wait state and nobody in this example notifies the thread in wait state. Hence the program hangs.

Section 17.12 - 17.14 of Java Lang Specification explains it all.
 
Ning Zhuang
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you very much, Susanta Chatterjee !
I rewrite the program and catch the IllegalMonitorStateException.
I have read $17 but still not understand.
thread no1 first run, and enter the synchronized block, then wait and release the lock, then no2 get it and enter, then wait and release the lock too.
Why the exception is throwed?
And if I change it to this ,compile and runs just fine. It seems that ss do act as a lock .

class AA implements Runnable{

static String ss=new String();
public void f(){
try{
synchronized(ss){
System.out.println("hello");Thread.sleep(2000);}
}
catch(Exception e){
e.printStackTrace() ;
}
}

public void run(){f();}

public static void main(String[] args) {
AA a =new AA();

new Thread(a,"no1").start();
new Thread(a,"no2").start();
}}
 
Ranch Hand
Posts: 145
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all !

Ning i rewrite ur code :



What i have made a change is that i have used ss.wait inside of wait, and the program will wait forever.

I will try to explain you abt your query.Hope it helps u :

Explaination : You are synchronizing thread on ss object and calling
wait() method which implies you are calling on this object which 'a' is refering,but this object does not hold a lock as in synchronized method the argument passed is ss object.So,IllegalMonitorStateException is thrown.

Is it Ok !! Let me know
 
tanu dua
Ranch Hand
Posts: 145
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry Ning !!

After the output the line should be like :

"What i have made a change is that i have used ss.wait instead of wait, and the program will wait forever."
 
Ning Zhuang
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
tanu dua ,
I very much appriciate your help.

now, I'v completely unstood the concept.
Thank you very very much for your explanation(just to the point). and thanks to Susanta Chatterjee!
Best wishes.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic