• Post Reply Bookmark Topic Watch Topic
  • New Topic

wait() and sleep() - looking for suggestions

 
mari bo
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 54
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4181
22
IntelliJ IDE Java Python
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"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:
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!