aspose file tools
The moose likes Threads and Synchronization and the fly likes wait() and sleep() - looking for suggestions Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Java » Threads and Synchronization
Reply Bookmark "wait() and sleep() - looking for suggestions" Watch "wait() and sleep() - looking for suggestions" New topic
Author

wait() and sleep() - looking for suggestions

mari bo
Greenhorn

Joined: Feb 10, 2011
Posts: 7
Hi guys, nice to be here again!

I have a little question about threads. I have a project where there is a thread which
is doing something, and that "doing something" is simulated by sleep().
I have to implement a way to stop for a moment that "doing something" by a wait() call,
time in which the thread would save its state(to be implemented in a second moment).

I tried to implement that in the following way:



Actually, it doesn't stop the 5 seconds sleeping, but it waits for the sleep to finish and then it simulates the stopping.
My question is:

do you know a nice way to tell the sleeping to stop with the wait() calling?

Thank you very much for your attention!

maribo
Nomaan Butt
Ranch Hand

Joined: Oct 19, 2011
Posts: 54
this is quite old post but still i would like to answer,

I have to implement a way to stop for a moment that "doing something" by a wait() call,
time in which the thread would save its state(to be implemented in a second moment).


if i have understood correctly then the query is you need to stop for all the threads to finish and then save their states.

this can be done using CyclicBarrier. If i have understood the query correctly.
Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 1900

"do you know a nice way to tell the sleeping to stop with the wait() calling?"

You can't get sleep() to stop with a wait() call, you need to interrupt the Thread which is sleeping.



In this case, you probably would not want to print the stack trace when you catch the InterruptedException in ThreadA's run() method. Since the InterruptedException happens as a side effect of normal operation you would just catch the error and do nothing:


Steve
 
 
subject: wait() and sleep() - looking for suggestions
 
Threads others viewed
Threads interview questions
Thread and Synchronization
Question regarding notify() and wait()
Using wait/notify()
MyEclipse, The Clear Choice