• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

wait() and sleep() - looking for suggestions

 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Bartender
Posts: 4179
22
IntelliJ IDE Python Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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:
 
reply
    Bookmark Topic Watch Topic
  • New Topic