aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Thread Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Thread " Watch "Thread " New topic
Author

Thread

Nabila Mohammad
Ranch Hand

Joined: Nov 05, 2007
Posts: 661
I am having a little problem understand the concept of threads.
Particularly the words like locking and synchronising.





This is the code from K&B
I cannot understand how the program is running.

After b.start() is called - ThreadB is invoked and starts running.
Now the thread A is supposed to continue irrespective of Thread B.

However it is synchronised and has a lock on Thread B [ I am not sure what this means]

Because I thought it was only after b.wait() was called , thread A would wait till thread B is over.
And that would mean System.out.println("Waiting for b to complete...") would be executed.

But here, the entire block is on hold till Thread B is completed.


Can some one tell me what do you mean by
"Thread A must own a lock on B.For a thread to call wait() or notify(), the thread has to be the owner of the lock for that object"

Which thread is it talking about?

When b.wait() is called,Is Thread b locked or THread A and which Thread is "waiting"?


The future belongs to those who believe in the beauty of their dreams.Dream BIG!
Swathi Kota
Ranch Hand

Joined: Jun 04, 2008
Posts: 52
I too have the same problem. I did not get the concept of locks. I always land up in confusion as to which Thread needs to own lock on which object. I referred to some more books for threads but it further increased my confusion.


SCJP 6, SCWCD 5
Success is how high you bounce when you hit bottom
Abdul Mifraz
Greenhorn

Joined: Mar 27, 2006
Posts: 5
it's simply like this:

when ever the code meets a line synchronized( <ref> ) the code should
then get a lock on the <ref> we have provided. without the lock the
code cannot continue from there. so it will wait till the lock is allocated
to the thread witch requesting it. so for the example



hope this help...


[ June 04, 2008: Message edited by: Abdul Mifraz ]

[ June 04, 2008: Message edited by: Abdul Mifraz ]
[ June 04, 2008: Message edited by: Abdul Mifraz ]

SCJP 5.0(87%)
Ram Reddy
Ranch Hand

Joined: Feb 20, 2007
Posts: 88
Hi Nabila Mohammad, Swati

I slightely modified the code

class ThreadA
{
public static void main(String...strings)
{
ThreadB b = new ThreadB();
b.start();
System.out.println("Before Acquiring lock on 'b' Object in ThreadA"); // Added this statement //line1
synchronized (b) {
try
{
System.out.println("Wating for b to complete...");
b.wait();
}
catch(Exception e)
{

}
System.out.println("Total is " + b.total);
}
}
}
class ThreadB extends Thread
{
int total;
public void run()
{
System.out.println("In the run method of ThreadB"); // Added this statement //line2
/*try //line 3
{
Thread.sleep(1000);
}
catch(Exception e)
{

}*/
synchronized(this)
{
System.out.println("Thread At Work");
for(int i = 0; i < 100; i++)
{
total = i;
System.out.println("Total is " + total);
}
System.out.println("Thread Not Working");
notify();
}
}
}

First ThreadB 'b' is created in the main method. then after b.start() , it is eligible for running state.At this point 'b' may execute or main thread may execute or both one after the other.

Scenario 1:-

after calling b.start() main thread is executing, then the statement at //line will be printed on the console. then main thread acquires lock on 'b' object. then its starts executing try block and "Wating for b to complete..." will be printed.
then b.wait() is called , main thread releases lock on 'b' object and is waiting for someone to call notify on 'b' object. When notify() is called on the 'b' object then only main thread is eligible to running.

In this case we have notify() in the ThreadB class. So main thread will wait untill notifiy() is executed.

Scenario 2:-

After calling b.start() , threadB's run() method is starts executing. Then "In the run method of ThreadB" will be printed on the console at //line2.

Then in the run method , Current Object ( b in this case) acquires the lock on itself. Then it starts executing the synchronized block. At the last statement it calls the notif() on itself. So the most recent thread which is waiting for the lock to release is now eligible to running.

Before acquiring the lock ( b object) on itself if main thread executes b.wait() , then main thread prints "Total is 99" at last.

After acquiring the lock ( b object) on itself if main thread wont executes b.wait() , then main thread will wait infinetely.


Scenario 3:-
If you uncomment the code at line3 , then always b.wait on the main thread executes before printing "In the run method of ThreadB" on the run() method of threadB at //line2
Nabila Mohammad
Ranch Hand

Joined: Nov 05, 2007
Posts: 661
Thanks for your help - Abdul and Rami,

I did get some part of it...but still pretty confused.
Can you please elaborate on the term "Aquiring a lock (on b - for eg.)

Does that mean, that now only thread b will be running or will it be thread B will Not be running..
Or neither.

From what i understood b.wait() means
That the current thread that is running(ie. Thread A) will stop and the execution will get transferred to Thread B. untill notify() is called.
[ i guess that's what you call releseign the lock on B - I am still pretty confused. Please clarify If I am going wrong somewhere]

But what happens when Thread B runs and executes notify() before wait() is invoked.


It would be nice if some gives a rough anaolgy related to Thread and lockign and Synchronisation.

Will make life much easier
Swathi Kota
Ranch Hand

Joined: Jun 04, 2008
Posts: 52
Hey Thanks a lot for your efforts to explain the concepts.My understanding has improved but i guess i need much practice to understand these concepts.
Nabila Mohammad
Ranch Hand

Joined: Nov 05, 2007
Posts: 661
Hey Swati,

Do you have any link that can help with this - anything and everthing related to this topic

I am still pretty Muddled up..
Nabila Mohammad
Ranch Hand

Joined: Nov 05, 2007
Posts: 661
Before acquiring the lock ( b object) on itself if main thread executes b.wait() , then main thread prints "Total is 99" at last.

After acquiring the lock ( b object) on itself if main thread wont executes b.wait() , then main thread will wait infinetely.



what does this mean?
Ram Reddy
Ranch Hand

Joined: Feb 20, 2007
Posts: 88
Hi mohammad,

If main thread executes the b.wait() , then main thread is waiting for some other thread to notify() the thread(s) which are waiting on b.

Scenario 1:- if main thread executes the b.wait() before notify() statement in run() method of ThreadB , then main thread is waiting and b object acquires lock on iteslf and it defineltely notifies the main thread. So main thread is now eligible to running.

Scenario 2:- If b object thread executes the notify() method and main thread is not executed the b.wait() yet, so b object thread dont have to notify any thread(s) and it's run method completes the execution. After main thread get execution call and it executes the b.wait() method() , then main thread goes to waiting/ blocked state. At this time already b object thread completes its run() method , so there is no call to notify the threads which are waiting on 'b' object. So main thread waits indefinetely
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Thread