File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Having doubts about an example from topic Threads in KB. 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 "Having doubts about an example from topic Threads in KB." Watch "Having doubts about an example from topic Threads in KB." New topic

Having doubts about an example from topic Threads in KB.

Anuj Prashar
Ranch Hand

Joined: Apr 15, 2008
Posts: 99

Hi guys!Please clear my doubt in following example of threads in KB.

1. class ThreadA {
2. public static void main(String [] args) {
3. ThreadB b = new ThreadB();
4. b.start();
6. synchronized(b) {
7. try {
8. System.out.println("Waiting for b to complete...");
9. b.wait();
10. } catch (InterruptedException e) {}
11. System.out.println("Total is: " +;
12. }
13. }
14. }
16. class ThreadB extends Thread {
17. int total;
19. public void run() {
20. synchronized(this) {
21. for(int i=0;i<100;i++) {
22. total += i;
23. }
24. notify();
25. }
26. }
27. }

Now when line 4 is executed there are 2 threads-main thread & thread running for loop.Thread running for loop acquire lock on object b therefore main thread cannot enter synchronized block in line 6 until other thread is completed releasing lock on object b which main method can use to call wait method.What exactly happens is main thread execute b.wait() simultaneously or it wait for other thread to complete before entering synchronized block.


Yeray Santana Borges
Ranch Hand

Joined: Mar 02, 2008
Posts: 46
Hello Anuj,

I see the syncronized keyword like an atomic operation, only one thread got the synchronization on the same object, not is possible that two threads run the synchronization simultaneously. Once the main thread or the threadB got the lock for the same object.

In this example if threadb got the lock for b object before the main thread, main thread wait on line 6. After Thread b finish his synchronized block, the lock is released and the main thread wait forever to a notify().

try to run this code:

The main thread call wait() after a notify(), so main thread wait forever in his wait() method. An spurios wake up can success too, but i dont know what exactly happen with the main thread in that situation, i think that the main thread is resumed and variable is printed but i not sure, see the Object API

Bob Ruth
Ranch Hand

Joined: Jun 04, 2007
Posts: 320
Just another point of view....
understand that, when you call the start() on the new thread, nobody says that the new thread is going into execution right now. THAT is up to the JVM thread scheduler. All the start really does is move the new thread in to the runnable state.

In the code that you wrote there are two possibilities:
a) when the main thread calls b.start(), the new thread will be moved to runnable state and the thread dispatcher will dispatch it immediately and take the lock (which is the path that you seem to be assuming in the body of your question), and

b) when the main thread calls b.start(), the new thread will be moved to runnable and the thread dispatcher will return to the main thread. In that case the main thread will hit the lock first.

The important thing to grasp is: calling start does NOT force execution of the new thread, it simply changes the new thread to runnable so that, at some time, the thread dispatcher CAN put it into execution. But it is up to the dispatcher when it will do so. Your code must be tolerant of either possibility.

SCJP - 86% - June 11, 2009
kaushik vira
Ranch Hand

Joined: Feb 01, 2007
Posts: 102
Consider This Example:-

public class ThreadA {
public static void main(String [] args) {
ThreadB b = new ThreadB();

synchronized(b) {
try {
System.out.println("Waiting for b to complete...");
System.out.println("after wait");
} catch (InterruptedException e) {}

System.out.println("Total is: " +;

class ThreadB extends Thread {
int total;

public void run() {
System.out.println("thread B in run");
}catch(InterruptedException e){


synchronized(this) {
for(int i=0;i<100;i++) {
total += i;

As per me when you call wait() it`s also release the lock from that sync object and go to sleep until other Thread not notify that Thread.

Still i will explore some about it, and try to clear it.

kaushik Vira
SCJP, Preparing SCWCD..
I agree. Here's the link:
subject: Having doubts about an example from topic Threads in KB.
It's not a secret anymore!