• 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
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

threads

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
thankyou
 
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
Thanks,
Adrian
 
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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,
Corey
 
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Great explanation Corey,
I haven't studied the subject of threads yet, but your explanation was clear even to a thread newbie like me.
Brian
 
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
Regards,
Raman
 
Roopa Gowda
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thankyou Adrian and Corey for a very good explanation.
 
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Good one!!
 
Corey McGlone
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Raman,
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.
 
If you open the box, you will find Heisenberg strangling Shrodenger's cat. And waving this tiny ad:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic