• 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:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

Thread synchronization doubt

 
Ranch Hand
Posts: 486
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator




Can anyone please expain wait() for me.All I know about wait() is the thread which invoke wait() method will giveup its lock.But here,how come line(1) will be waiting forever and also please explain the execution step by step.
 
Ranch Hand
Posts: 809
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator



Main Thread gets the lock of String array object as it enters synchronized block.

At line 1, main thread releases the lock and comes in waiting state.

Since you don't have any other thread which is notifying this thread, so this thread will wait forever.

Regards

Naseem
 
Shiva Mohan
Ranch Hand
Posts: 486
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for the reply Naseem.So the Main thread release its lock on line (1).

Is that waiting state means,IllegelMonitorStateException.If not,what is IllegelMonitorStateException?

Could you please help me how can I notify this main thread and print number 3 too.In other words,How could the program come out of the waiting state.

Please bear with my doubts since thread is a new and tough concept for me to get.
 
Naseem Khan
Ranch Hand
Posts: 809
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Have a look on this code

Code is self explanatory

 
Naseem Khan
Ranch Hand
Posts: 809
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
IllegalMonitorStateException is thrown to indicate that a thread has attempted to wait on an object's monitor or to notify other threads waiting on an object's monitor without owning the specified monitor.

e.g.,


or



Both the above code will compile fine, but throws exception at runtime: IllegalMonitorStateException as main thread is not the owner of the Object's monitor on which wait is called.

Surely your wait() method will not throw IllegalMonitorStateException as main thread is the owner.

Regards

Naseem
 
Shiva Mohan
Ranch Hand
Posts: 486
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thankyou very much for your detailed coding Explanation.I got it.Thanks again.
 
Ranch Hand
Posts: 142
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What happens when two String objects are created as
String str1 = "Hello";
String str2 = "Hello";
This creates only one String object.

If we acquire the lock as syzchronized(str1), then is that
syzchronized(str2) is also affected as there can be one lock per Object
[ June 20, 2006: Message edited by: S Thiyanesh ]
 
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by S Thiyanesh:
String str1 = "Hello";
String str2 = "Hello";
This creates only one String object.

[ June 20, 2006: Message edited by: S Thiyanesh ]



Congtatz !! U nailed this one. Remember in JVM pool, Strings are optimized agressively. Thats wat I remember from my SCJP when I read the JVM specs.
So above code **WILL** create just one monitor. Even though they may be many references to it. Dont care about them


Beware of writing like this. This will break.
String str2 = new String("hello");

Cheers
 
Nishant Verma
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by S Thiyanesh:

If we acquire the lock as syzchronized(str1), then is that
syzchronized(str2) is also affected as there can be one lock per Object
[ June 20, 2006: Message edited by: S Thiyanesh ]



Correct. If anyone has a lock on str1 object. Then no other thread can dare touch "hello" object. So whoever calling synchronized(str2) will be blocked.

Cheers
reply
    Bookmark Topic Watch Topic
  • New Topic