0% found this document useful (0 votes)
5 views4 pages

Java Thread Synchronization Issues

The code sample is confusing the order in which the main thread and new thread execute. Synchronizing access to the shared object before starting the new thread would fix the potential for the notify to happen before the wait. The wait is allowing the main thread to pause execution until notified by the new thread that it has completed.

Uploaded by

pavani21
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views4 pages

Java Thread Synchronization Issues

The code sample is confusing the order in which the main thread and new thread execute. Synchronizing access to the shared object before starting the new thread would fix the potential for the notify to happen before the wait. The wait is allowing the main thread to pause execution until notified by the new thread that it has completed.

Uploaded by

pavani21
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

6/13/13

A Thread problem (Threads forum at JavaRanch)

File APIs for Java Developers Manipulate DOC, XLS, PPT, PDF and many others from your application. [Link]

Big Moose Saloon


A friendly place for programming greenhorns!
Search

Java FAQ

Recent Topics

Register / Login

A special promo: Enter your blog post or vote on a blogger to be featured in an upcoming Journal

JavaRanch Java Forums Java Threads and Synchronization

Author

A Thread problem
posted 6/3/2008 5:25 PM

Yu Chen Greenhorn Joined: Jun 03, 2008 Posts: 2

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. [Link](); 5. 6. synchronized(b) { 7. try { 8. [Link]("Waiting for b to complete..."); 9. [Link](); 10. } catch (InterruptedException e) {} 11. [Link]("Total is: " + [Link]); 12. }

[Link]/t/234233/threads/java/Thread

1/4

6/13/13

A Thread problem (Threads forum at JavaRanch)

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 [Link]() in line 9 do??? Sorry about my english.

Peter Chase Ranch Hand Joined: Oct 30, 2001 Posts: 1970

posted 6/3/2008 5:59 PM

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.

Betty Rubble? Well, I would go with Betty... but I'd be thinking of Wilma.<br /> <br />#:^P

Yu Chen Greenhorn Joined: Jun 03, 2008 Posts: 2

posted 6/3/2008 6:19 PM

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 Joined: Dec 11, 2004 Posts: 13410


I like...

posted 6/3/2008 6:22 PM

"Yu is not Chen Yu Chen", Please check your private messages regarding an important administrative matter. -Ben
2/4

[Link]/t/234233/threads/java/Thread

6/13/13

A Thread problem (Threads forum at JavaRanch)

Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf Nicholas Jordan Ranch Hand Joined: Sep 17, 2006 Posts: 1282

posted 6/4/2008 6:16 AM

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 [Link]() 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 [Link]() code.

"The differential equations that describe dynamic interactions of power generators are similar to that of the gravitational interplay among celestial bodies, which is chaotic in nature." Ashish Vijay Joshi Greenhorn Joined: Nov 27, 2006 Posts: 10

posted 6/16/2008 2:22 PM

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

I agree. Here's the link: [Link]

subject: A Thread problem

Similar Threads wait() will wait forever Confused about Thread synchronization Threads (wait method) Having doubts about an example from topic Threads in KB. Thread wait() and notify()
[Link]/t/234233/threads/java/Thread 3/4

6/13/13

A Thread problem (Threads forum at JavaRanch)

All times above are in your local time zone & format.T he current ranch time (not your local time) is Jun 13, 2013 03:31:38 .

Contact Us | Powered by JForum |

C opyright 1998-2013 Paul W he aton

[Link]/t/234233/threads/java/Thread

4/4

You might also like