• Post Reply Bookmark Topic Watch Topic
  • New Topic

A Thread problem  RSS feed

 
Yu Chen
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi, all

This is my first post here.

I am reading <sun certified programmer for java 5>, and the codes below really confused me
======================================
1. class ThreadA {
2. public static void main(String [] args) {
3. ThreadB b = new ThreadB();
4. b.start();
5.
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: " + b.total);
12. }
13. }
14. }
15.
16. class ThreadB extends Thread {
17. int total;
18.
19. public void run() {
20. synchronized(this) {
21. for(int i=0;i <100;i++) {
22. total += i;
23. }
24. notify();
25. }
26. }
27. }
======================================
My question is when ThreadB is running in line 4, object b simply aquire the lock of itself, then main thread has to wait. Then what does b.wait() in line 9 do???

Sorry about my english.
 
Peter Chase
Ranch Hand
Posts: 1970
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Where did that program come from?

I am fairly sure that it has a bug in it. You should synchronise before starting the thread. Otherwise, the notify() might happen before the wait(), meaning the program will hang forever.
 
Yu Chen
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
haha

That's what I am confusing about...

I got this from <SCJP Sun Certified Programmer for Java 5 Study Guide (Exam 310-055) (Certification Press Study Guides)>

Seems like it is a highly recommended book -_-
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"Yu is not Chen Yu Chen",
Please check your private messages regarding an important administrative matter.
-Ben
 
Nicholas Jordan
Ranch Hand
Posts: 1282
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Don't worry about poor english, the question is clear. What this code is doing is showing that the execution described by the code in the main method is waiting for the code described in the ThreadB.run() to complete. There are two separate areas of activity here, machine activity rather than the code you see before your eyes.

One of those describes a sequence of machine activity, so does the other. Somewhat like two computers running at once. The wait in line nine is to allow the machine running in the main() to await completion of the actvity described by the ThreadB.run() code.
 
Ashish Vijay Joshi
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here once ThreadB is started at line 4 depending on VM either run inside new thread or synchronized block in main thread at line 6 will execute.
Suppose synchronized block in main executes it acquires a lock and immidiately calls wait() on this object, resulting in release of the acquired lock in line 6. Now if run method of ThreadB executes it gets lock on instance b and does the work and notifies on b which could invoke thread(here main)from blocked state to runnable. But the other way i.e if VM chooses to execute run inside ThreadB before synchronized block code in main could be hazardous. Hope this helps.

Ashish
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!