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 synchronization doubt 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 "synchronization doubt" Watch "synchronization doubt" New topic
Author

synchronization doubt

Sreeraj Harilal
Ranch Hand

Joined: May 09, 2006
Posts: 45
class Reader extends Thread
{
Calculator c;
public Reader(Calculator calc)
{
c = calc;
}
public void run()
{
synchronized(c)
{
try
{
System.out.println("Waiting for calculation...");
c.wait();
} catch (InterruptedException e) {}
System.out.println("Total is: " + c.total);
try
{
c.wait(); //doubt
}
catch (Exception e)
{
}
System.out.println("Why the wait method is not executing again.");
}
}
public static void main(String [] args)
{
Calculator calculator = new Calculator();
new Reader(calculator).start();
new Reader(calculator).start();
new Reader(calculator).start();
calculator.start();
}
}

class Calculator extends Thread
{
int total;
public void run()
{
synchronized(this)
{
for(int i=0;i<100;i++)
{
total += i;
}
notifyAll();
}
}
}


Please give me an explanation that why that(//doubt) wait() method has no effect.


SCJP 5.0<br />SCWCD 1.4<br />SCBCD 1.3<br />SCEA part 1
Naseem Khan
Ranch Hand

Joined: Apr 25, 2005
Posts: 809
Hi...
A very gud point u have made.

First of all is it problem putting codes in code tag

come to ur question.

A thread returns from wait method when another thread notifies.

But even if a thread waits for notification and then another thread acquires the same lock and enter some other synchronized block and without notifying if it leaves dat block then in dat case also the first thread returns simply frm wait method.

This is wat its happening in ur case.

All thread returns frm first wait() due to notifyAll().
Second time, all thread returns frm second wait coz of the termination of second synchronized block.

I can give u sample code jst see it there is no notifyAll with one wait method here but still thread returns frm that wait method.




output of this code is....

1.Thread 1 entered synchronised block
2.Thread 1 is jst going to wait...
3.Thread 2 entered synchronised block
4.Thread 2 is jst going to wait...
5.Thread 3 entered synchronised block
6.Thread 3 is jst going to wait...
7.In synchronized block of calculator thread
8.Outside synchronized block of calculator thread
9.Thread 1 returned frm wait...
10.Thread 2 returned frm wait...
11.Thread 3 returned frm wait...

--------------------------------------------------------

Till line 6, all threads are waiting.
at line 7, calculator thread entered synchronized block.
at line 8, calculator thread comes out of the synchronized block without notifying.
As it come out, all threads 1, 2, 3 simply return frm wait method.


plz give ur suggestions to this reply.


regards


Naseem Khan
[ May 20, 2006: Message edited by: Naseem Khan ]

Asking Smart Questions FAQ - How To Put Your Code In Code Tags
wise owen
Ranch Hand

Joined: Feb 02, 2006
Posts: 2023
Your code will get randomly results. Sometime, it will print out "Why the wait method is not executing again." and sometime not.

The reason is the Calculator Thread. The first time the thread call notifyAll(), it will wake up all waiting threads Reader(s) (I assume that all Reader threads will run before Calculator thread) and they will print out the total count number.

After that there are two cases may happen:

1) the Calculator thread return from the run() method. Then all of Reader run to call wait() method. In this case, all Reader threads will wait there. You will not see "Why the wait method is not executing again."

2) All of Reader threads run to call wait() method first and then the Calculator thread runs end of run() method and when it ends the run() method it will notifyAll other waiting threads. You will see the results printing out.

In the first case, the notify even raises before other threads call wait().
Naseem Khan
Ranch Hand

Joined: Apr 25, 2005
Posts: 809
Hi Wise,
The reply which i have posted is it wrong??

The code which i have posted give that output always.



After that there are two cases may happen:

1) the Calculator thread return from the run() method. Then all of Reader run to call wait() method. In this case, all Reader threads will wait there. You will not see "Why the wait method is not executing again."

2) All of Reader threads run to call wait() method first and then the Calculator thread runs end of run() method and when it ends the run() method it will notifyAll other waiting threads. You will see the results printing out.



The second case which u have discussed, even if there is no second notifyAll method, if run returns, then it will notifyAll. is is wat u want to say?


waiting for ur reply

regards

Naseem Khan
[ May 20, 2006: Message edited by: Naseem Khan ]
wise owen
Ranch Hand

Joined: Feb 02, 2006
Posts: 2023
Hi, Naseem:
Your code is nothing wrong. Your code is not exactly like Sreeraj's. Sreeraj' code has a second wait() method call in Reader class and also has notifyAll() call in Calculator.

Thx, Wise
Naseem Khan
Ranch Hand

Joined: Apr 25, 2005
Posts: 809
Its difficult to predict the output in his case as different output I was getting in his code. So I simplified his problem by removing one wait and one notifyAll.
In his case control returns frm first wait due to notifyAll. For second wait there is no notify method, then how thread returns frm it. This was his question.

In my case there is no notify with one wait method, then how thread returns from it without notify. That I tried to explain frm the output which I got and the result says that...

"Yes without notify, a thread can return frm wait method, re acquire lock again and do further processing." frm my output its very clear.


Thanks & Regards


Naseem Khan
wise owen
Ranch Hand

Joined: Feb 02, 2006
Posts: 2023
This thread.
Naseem Khan
Ranch Hand

Joined: Apr 25, 2005
Posts: 809
Thanks wise,
The information given on this thread is very gud and in fact is the same question which was posted by Sreeraj Harilal.

From that thread, its now clear that if any thread returns from run method, it issues notifiesAll as a part of clean up to all waiting threads.

Thanks one again

Naseem Khan
Sreeraj Harilal
Ranch Hand

Joined: May 09, 2006
Posts: 45
Thanks Wise Owen and Naseem Khan.

Actually my doubt was
A synchronized method leaves his lock calling wait() method.thats ok
Can that method gets the lock again when another thread notifies?
sankar adabala
Greenhorn

Joined: Feb 24, 2004
Posts: 8
Hi Wise Owen,

Here is the code change that reflect your second case:
" All of Reader threads run to call wait() method first and then the Calculator thread runs end of run() method and when it ends the run() method it will notifyAll other waiting threads. You will see the results printing out"

To make sure that Readers call the second wait first followed by the Calculator completing the run method i have made the calculator thread sleep for some time.

Now the 2nd notify as said by you will called as a cleanup process when the calculator run method terminates and those readers which r waiting on the second call will be notified and o/p follows

class Calculator extends Thread
{
int total;
public void run()
{
synchronized(this) {
for(int i=0;i<100;i++) {
total += i;
}
notifyAll();
try {
Thread.currentThread().sleep(1000);
} catch(InterruptedException ie) {
System.out.println(" Interrupted Exception occurred");
}
}
}
}

The o/p obtained is as follows:

Waiting for calculation...
Waiting for calculation...
Waiting for calculation...
Total is: 4950
Total is: 4950
Total is: 4950
Why the wait method is not executing again.
Why the wait method is not executing again.


Sankar Krishna Adabala
 
 
subject: synchronization doubt
 
Similar Threads
doubt in thread: notifyAll()
blocking and waiting
Doubt with notify/ notifyAll method
blocking and waiting
wait(), notify(), notifyAll() doubt