• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
Bartenders:
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

See this thread interaction code

 
Ranch Hand
Posts: 169
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
We have this program on K&B book:



I didnt get whats happenign in this code.

Getting the output:
-----------------------------
Waiting for b to complete...
Total is: 4950
-----------------------------

On line we are startign the thread, tha tmeans control will come to line 19 and calculates the totals in teh for loop, and when it comes to line 24 there is notigy() method. In order to call notify() method, we should have called wait() method on that particular object. But we are calling notify() method from run() method itself, but calling wait() method at line 9 which will be executed after run() method completion.

And why gettig the above output??

I didnt get whats happening in this code at all, Pls explain me thouroughly .....

Thanks.
 
Ranch Hand
Posts: 127
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Calling a start method on a thread object does not mean it will start running. Calling start puts a thread into runnable state and it's up to scheduler when the thread really start running (it's run() methods runs).
 
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi!
I read thoroughly whats written in book n saying this....
I understand that after start method is called , this code does'nt go with its run method. Instead ThreadA continues with synchronized block.
(I too wonder as to what the reason is!!)But this exactly what happenning.
Now ThreadA owns the lock of ThreadB.When it calls wait on b , it releses its lock and waits for ThreadB to complete and notify it.
When it is notified it prints total n there code ends.
 
Ranch Hand
Posts: 104
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There two threads in the program(Main and Child)

Basic requirements:
to call wait() you need to have the lock over the object(here "b")
to signal notify(), lock is needed on the object(here "b")

Scenario 1:
Main Thread continues execution after calling b.start();
In line 6 Main thread acquires the lock on Object "b";
In line 9 it waits on the object(b) and releases all the lock on "b".
Now the Child thread excutes the line 20 synchronized(this) and
acquires lock on this ("b")
Calculates the total and signals the notification for the threads
waiting on the current object("b")
Then it releases the lock after exiting from run()
So the main thread is NOTIFIED.
Now the Main Thread executes and prints the total

Scenario 2:
The Main thread executes the b.start().
The threads are switched and now the Child thread executes.
Child thread locks on itself in line 20, so the Main thread can't
execute line 6 until the lock is released on "b".
It calculates the total and signals notify() for the threads waiting on
on this object "b"(but currently no thread is waiting on "b")
Now synchronized block completes and lock on "b" is released
Then Main Thread executes and acquires lock on "b" in Line 6.
In line 9 it calls wait() on b and releases all its lock on "b".
But by now there is no process running on "b" so it waits
forever.

Either of this scenario may take place based on which thread acquires lock
For the scenario "2" to take place every time
Insert the following code at line 5
<code>
try{
b.join();
}catch(InterruptedException ie){
System.out.println("Interrupted by Depricated function");
}
</code>
[ March 28, 2006: Message edited by: Shaliey G ]
 
Ja vardhan
Ranch Hand
Posts: 169
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Shaliey, Swapna, Alexander (this is teh order whose explanation helped me much ) thanks for your replies. I understood the code now.

I didnt clearly understand the concept of making synchronized blocks (I understood about synchronized methods). What is the object we are putting in to synchronized code... like synchronized(this) , synchronized(b) , and some where I found synchronized(someStringObject) (can we give give String object name?? )

Pls explain in detail about synchronized blocks.

Thanks.

[ March 28, 2006: Message edited by: Ja vardhan ]
[ March 28, 2006: Message edited by: Ja vardhan ]
 
When it is used for evil, then watch out! When it is used for good, then things are much nicer. Like this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
    Bookmark Topic Watch Topic
  • New Topic