• 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

Synchornized Block..

 
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Dear frineds,
Can any one explain the following code..

regards
Suresh
 
Ranch Hand
Posts: 1157
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Suresh,
In your code, you are spawning a thread and running the holdIT(String) method in the new Thread created.
In the holdIT() method, you are making the thread to wait.Since there is no notify() for this Thread, it will wait for infinite time.As a result, the program will not terminate - the Main thread will wait for the newly spawn thread to complete for an infinite time.
Do you want me to elaborate on anything specific?
-- Sandeep
SCJP2, OCSD(Oracle JDeveloper), OCED(Oracle Internet Platform)
 
suresh seeram
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Dear Sandeep,
Thanks for fast response..
at comment//1 if i change the above code this.wait() to ref.wait()..
Now please explain, what is the difference between first code and this code..

regards,
Suresh
[This message has been edited by suresh seeram (edited October 21, 2001).]
 
Desai Sandeep
Ranch Hand
Posts: 1157
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Suresh,
Good question!
As regards the output, I donot think it should make the difference - the main thread would still wait infinitely for the spawned thread to complete.
However, I am not sure if you would really want this to happen.If you want to apply wait() on the ref Object, it is advisable to obtain a monitor on the same, by declaring an instance variable for it and then obtaining a monitor on it.Your code should look like this :

The above code should allow normal execution of the program.The above code means :

  • Lock on the current Runnable object is not obtained
  • You need to monitor (synchronize) the execution the thread unsafe methods of Object class.

  • Also note in your code, when you say ref.wait(), it will not serve purpose of Thread safety;Synchronized becomes redundant in that case, as every thread has its own instance of local variables.So ref would be a local variable for the newly spawn thread.That's why obtaining a lock on the Runnable object doesnot make sense!
    Let me know if you have further queries.
    Good Luck,
    Sandeep
    [This message has been edited by Desai Sandeep (edited October 21, 2001).]
 
Ranch Hand
Posts: 5399
1
Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Desai Sandeep:
Hi Suresh,
the main thread would still wait infinitely for the spawned thread to complete.


Hi sandeep
but I think that main thread will terminate after spwaning the new thread.
If we want to main thread to terminate after spawned thread then we should join the thread.
correct me if i am wrong.
I have one que also?
What is the difference when we say in the above code :
this.wait(); and ref.wait();
where can i get good material on object locking on net ??


------------------
Regards
Ravish
 
Desai Sandeep
Ranch Hand
Posts: 1157
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Ravish,
It seems we have got confused between the Main Thread as a normal Thread (which executes
the main method of Java) and the Main daemon thread.
The Main daemon thread is the thread which I was refering to.The program Main thread is a
lightweight thread just like the newly spawn Thread.
To give an example, consider the following code :

You will see the output as
<pre>
I want to wait!!
Main Continues
</pre>

However, you will still not see the prompt, which signifies the normal completion of the
program.This means the non-daemon Main thread had completed its execution,although the
daemon thread is still waiting for the newly spawn thread to complete its execution - hence
the daemon Main Thread waits infinitely for this thread to complete!
The use of join serves a different purpose.It signifies the non-daemon main thread to wait
for the completion of the newly spawn thread.
So if we have :

You will see the output as
<pre>
I want to wait!!
</pre>

Note that "Main Continues" is never printed since the program main thread is waiting for
the newly spawn thread to complete!

In my earlier post, I had erroroneously mentioned that when we obtain a lock on the Object
(i.e. ref.wait()) we will have a normal termination of the program.The normal execution of
the program is only possible if the newly spawn thread is notified from its wait stage.
This is one of the possible ways to do it :


I believe my post is becoming rather too long! Once we are together on this, we will look
into the difference between this.wait() and obj.wait()!BTW, I suggest you think of an
example by taking hints from my previous post on this.
HTH,
Sandeep
SCJP2, OCSD(Oracle JDeveloper), OCED(Oracle Internet Platform)
 
R K Singh
Ranch Hand
Posts: 5399
1
Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks Sandeep...
thanks for giving me so much time....
it really helped me... and u were right i was thinking abt the main() thread.
Now here u spawaned a new question??
what is Daemon thread ??? I tried to know from some books ... but could not get the clear picture ?? and again any site(link) where i can find good DETAILS of thread.
still it is not clear to me what is the difference betn this.wait() and obj.wait()
Thanks for your time and patience to give answer to such silly questions.

------------------
Regards
Ravish
 
Desai Sandeep
Ranch Hand
Posts: 1157
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ravish,
I am stuck up with my work over here.I will come up with an example by the end of this week.In the meantime to understand the difference yourself, try to have 2 classes (A and B) with syncronized methods.In class B (which implements Runnable), use the syncronized block on instance of A and call the wait on A's instance.Also, in the run() implementation of B call a sychronized method of B's instance and see if it executes while executing the synchronized method of A.
Let me know if you are still not able to get it.
-- Sandeep
 
R K Singh
Ranch Hand
Posts: 5399
1
Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Sandeep
thanks a lot ...
Yes it is too late but... still thanks a lot ..
I have tried lot of permutation and combination of this.
will get back to you if still I have problem in threading...

[ January 17, 2002: Message edited by: ravish kumar ]
[ January 17, 2002: Message edited by: ravish kumar ]
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic