Meaningless Drivel is fun!
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes threads 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 "threads" Watch "threads" New topic


Roopa Gowda

Joined: Feb 24, 2004
Posts: 5
this is a question on threads

the answer is : ABC
how can u be sure that the thread t1 starts executing before the main thread completes its execution.
t1 is created by the main thread and hence its priority is same as that of the main
i think the answer can be either ABC or XYZ ??
can someone help me
Adrian Pang
Ranch Hand

Joined: Feb 20, 2004
Posts: 40
I think the answer is only ABC. There are a couple cases:
1) t1 starts after t1.start(), before synchronized (sa) in main:
In this case, sa[0] = "Not Done" and when t1 runs, the condition while (!sa[0].equals("Done")) is false and therefore it will call wait() and blocks, exiting the monitor. The main thread then runs and set the elements to ABC, then set sa[0] to Done and wake t1 up. Then, when t1 runs again, it will display ABC
2) t1 starts after synchronized (sa) in main: t1 blocks until main releases sa. After that, sa = {"Done", "A", "B", "C"}
Can you think of a case where t1 displays "XYZ"? The only case where I can think of is if t1 is interrupted, but that isn't the case in the code above.

SCJP 1.4, SCWCD 1.4, SCBCD 1.3
Corey McGlone
Ranch Hand

Joined: Dec 20, 2001
Posts: 3271
The output could never be "XYZ".
Let's look through the flow of the application a little bit. We start with this line:

Next, we create a new thread and pass it that String array. Once created, we kick off that thread.

Now this is where it can be a little tricky. What gets executed next? Is it going to next execute the synchonized block in main or the synchronized block in the new thread? Simply put, we just don't know.
Let's assume, that the new thread executes first. It acquires the lock on the String array object with this line:

Then in the next line, it checks the first index of the String array and compares it to the String literal "Done":

Well, at this point, sa[0] contains the String "Not Done". Therefore, this check succeeds (the inverse of sa[0].equals("Done"), which is false, is true). With that check succeeding, we jump into the while block and execute this line:

This causes this thread to move out of the running state and go into a waiting state. At this point, the new thread is done processing.
Now, with only one other thread to execute (that being the main thread), we know what's going to execute next. We'll next execute this line in the main method:

Because the new thread had invoked the wait method, it gave up the lock it had on the String array object. That allows the main thread to acquire it and move into it's synchronized block. The following lines are then executed:

This changes the contents of the String array object and, when done, performs a notify. There is only 1 thread waiting at this point so we know which thread is going to be awakened by this call. That is, of course, our new thread. When that thread wakes up, we pick up where it left off, after that wait() statement. As this was inside a loop, we again check the loop condition:

Now that the main thread modified the contents of our String array, we know that sa[0] does contain the String "Done". Therefore, this loop condition fails and we exit the loop. That takes us to this line:

Once again, the String array has already been modified so we now spit out "ABC".
Hopefully, you see that there is no way the output line can be executed before the String array has been modified by the main thread. The new thread will constantly wait for the main thread to complete before it can finish it's run method.
I hope that helps,

SCJP Tipline, etc.
Brian Albin

Joined: Mar 02, 2004
Posts: 24
Great explanation Corey,
I haven't studied the subject of threads yet, but your explanation was clear even to a thread newbie like me.
raman padma

Joined: Feb 20, 2004
Posts: 18
Hi Corey,
That was a soooo clear. U sure know ur stuff so well. I have exactly 10 more days to go for the exam. Still, I could not answer that question well. After i read ur explanation, i felt so much better. Do u suggest any other reading/ do u have any other sample programs like this for me to gothrough?
Roopa Gowda

Joined: Feb 24, 2004
Posts: 5
thankyou Adrian and Corey for a very good explanation.
Sindhur Sat

Joined: Feb 23, 2004
Posts: 23
Good one!!
Corey McGlone
Ranch Hand

Joined: Dec 20, 2001
Posts: 3271
If you have a cert prep book, I'm sure there's a section in there that covers multi-threading. You may just want to re-read that.
You can also check out this tutorial on Sun's site.
I agree. Here's the link:
subject: threads
It's not a secret anymore!