File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Synchornized Block.. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Synchornized Block.." Watch "Synchornized Block.." New topic
Author

Synchornized Block..

suresh seeram
Ranch Hand

Joined: Oct 06, 2001
Posts: 42
Dear frineds,
Can any one explain the following code..

regards
Suresh
Desai Sandeep
Ranch Hand

Joined: Apr 02, 2001
Posts: 1157
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)


<b>Sandeep</b> <br /> <br /><b>Sun Certified Programmer for Java 2 Platform</b><br /> <br /><b>Oracle Certified Solution Developer - JDeveloper</b><br /><b>-- Oracle JDeveloper Rel. 3.0 - Develop Database Applications with Java </b><br /><b>-- Object-Oriented Analysis and Design with UML</b><br /> <br /><b>Oracle Certified Enterprise Developer - Oracle Internet Platform</b><br /><b>-- Enterprise Connectivity with J2EE </b><br /><b>-- Enterprise Development on the Oracle Internet Platform </b>
suresh seeram
Ranch Hand

Joined: Oct 06, 2001
Posts: 42
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

Joined: Apr 02, 2001
Posts: 1157
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).]
R K Singh
Ranch Hand

Joined: Oct 15, 2001
Posts: 5371
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


"Thanks to Indian media who has over the period of time swiped out intellectual taste from mass Indian population." - Chetan Parekh
Desai Sandeep
Ranch Hand

Joined: Apr 02, 2001
Posts: 1157
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

Joined: Oct 15, 2001
Posts: 5371
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

Joined: Apr 02, 2001
Posts: 1157
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

Joined: Oct 15, 2001
Posts: 5371
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 ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Synchornized Block..