• 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
  • Ron McLeod
  • Paul Clapham
  • Tim Cooke
  • Devaka Cooray
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
Bartenders:
  • Carey Brown
  • Roland Mueller

synchronization doubt

 
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 809
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Ranch Hand
Posts: 2023
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 809
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 2023
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 809
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 2023
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This thread.
 
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
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
Posts: 45
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
God is a comedian playing for an audience that is afraid to laugh - Voltair. tiny ad:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic