• 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
  • Devaka Cooray
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Martijn Verburg
  • Frits Walraven
  • Himai Minh

confusing synchronized??

 
Ranch Hand
Posts: 156
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi evetyone, I am trying to run following program



It gives me output:
currently executing thread: Thread-0
currently executing thread: Thread-1
currently executing thread: Thread-2
currently executing thread: Thread-3

I thought output will be
currently executing thread: Thread-0

As as soon as Thread 0 enter in synchronized block it will have lock on B.class, hence other thread will be waiting for enter in synchronized block..
Its really confusing???
 
Ranch Hand
Posts: 504
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If I understood properly what the question was, I think the answer to it is:

you are putting the thread into the waiting state in call method, so as soon as the current thread goes into the waiting state it releases the lock and hence other threads can acquire the lock and continue.

now, I have a doubt.
does the program get lock on String object 'x' or B.class?
 
Sheriff
Posts: 9697
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

does the program get lock on String object 'x' or B.class?


That's right, the code synchronized on x not class B. Also you call x.wait() which releases the lock...
 
Neha Daga
Ranch Hand
Posts: 504
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks Ankit.

this chapter is really confusing.
 
Ranch Hand
Posts: 521
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
is there any need to put wait() there? as soon as the synchronize block gets over the lock is released isnt it....
 
rohan yadav
Ranch Hand
Posts: 156
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Ankit and Neha, understood now.
 
Neha Daga
Ranch Hand
Posts: 504
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Raju Champaklal wrote:is there any need to put wait() there? as soon as the synchronize block gets over the lock is released isnt it....



Yes, wait is unneccessary.

 
Ankit Garg
Sheriff
Posts: 9697
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Raju Champaklal wrote:is there any need to put wait() there? as soon as the synchronize block gets over the lock is released isnt it....


There's no "need" here, but that's a way to mix things up and see the behavior. In this program all the threads will wait on x object so the program will not terminate normally...
 
Ranch Hand
Posts: 2066
IntelliJ IDE Clojure Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm late and I've doubt in this code.
1) What is the neediness for the import java.util.*; statements?
2) Why is this program doesn't terminate normally?
 
Greenhorn
Posts: 3
Android Redhat Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
in response to your questions:
1) What is the neediness for the import java.util.*; statements?
2) Why is this program doesn't terminate normally?



1) The import of java.util.*; if not needed, the compiler will remove at compile time.

2) My believe is that the program does not terminate normally, because the application has 4 "user" threads that are still "waiting" for a notify event to be called.

Causes current thread to wait until another thread invokes the notify() method or the notifyAll() method for this object. In other words, this method behaves exactly as if it simply performs the call wait(0). ...



SCJP 6.0 exam scheduled for Feb. 1.
 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Abimaran Kugathasan wrote:I'm late and I've doubt in this code.
1) What is the neediness for the import java.util.*; statements?
2) Why is this program doesn't terminate normally?



Yea i too second that the import statement is unnecessary here.

The program would not terminate properly as each thread is waiting while never being notified at all. Please correct me if I am wrong here.

thanks,
Neha
 
Greenhorn
Posts: 21
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes nehaa you are right
the program will not terminate normally because all the threads are waiting on the "x" object and also there is no notify method so all the threads will continue to wait and the program wont terminate normally,But if the overloaded version of wait is used than program may complete normally.
 
I will open the floodgates of his own worst nightmare! All in a tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic