dcsimg
thread
3 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   Lak_Java
Posted On:   Wednesday, June 18, 2003 09:27 AM

How could you coordinate two threads that execute simultaneously inside synchronized methods of the same object?




1:&nbsp&nbsp Place the threads in the same ThreadGroup and let the ThreadGroup acquire the object's monitor

2:&nbsp&nbsp use Thread.wait() and Thread.notify()

3:&nbsp&nbsp use Thread.sleep()

4:&nbsp&nbsp use Thread.yield() to switch between threads at set points

5:&nbsp&nbsp dynamically control the priorities of the threads within the method

Re: thread

Posted By:   David_Bates  
Posted On:   Thursday, June 19, 2003 05:56 AM

PS - Do your own homework next time and stop wasting everyone's time.

Re: thread

Posted By:   David_Bates  
Posted On:   Thursday, June 19, 2003 05:55 AM

"two threads that execute simultaneously inside synchronized methods of the same object"



This isn't possible. In order for a thread to execute any synchronized code, it must first obtain the object lock. Only one thread may do this at a time. It is therefore impossible for two threads to be simulatneously inside synchronized methods of the same object.

Re: thread

Posted By:   Stijn_Fonck  
Posted On:   Wednesday, June 18, 2003 02:09 PM

Answer is 2: Coordination of threads is always done via wait and notify.

I you put a thread asleep, you aren't sure which other thread gets executed (it could also be the gc which starts executing).

Thread.yield() will try to see if there are some thread waiting with a higher priority, if not, this thread will restart executing eventually
About | Sitemap | Contact