• 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
  • Ron McLeod
  • Rob Spoor
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Henry Wong
  • Liutauras Vilda
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Tim Holloway
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Mikalai Zaikin
  • Piet Souris

how to create pausable threads

 
Ranch Hand
Posts: 101
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

I want to implement a service, where some work happens periodically in its own thread.
However outside client should be able to pause the thread for a while, and then resume it

I have been able to implement the same using ScheduledExcutorService and semaphore. however i am not satisfied with the implementation. As you can see in the code below, it has become necessar for the client to pass a semaphore instance to the running task.

is there a better way to implement the same

 
Stanley Walker
Ranch Hand
Posts: 101
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
sample output of the above code is

i am in doing smthing dont distubr 2018-06-25T21:51:49.623
i am in doing smthing dont distubr 2018-06-25T21:51:54.626
i am in doing smthing dont distubr 2018-06-25T21:51:59.628
i am in doing smthing dont distubr 2018-06-25T21:52:04.630
----clear waiting time...i am able to pause and resume-------
i am in doing smthing dont distubr 2018-06-25T21:52:29.558
i am in doing smthing dont distubr 2018-06-25T21:52:34.558
i am in doing smthing dont distubr 2018-06-25T21:52:39.559
 
Bartender
Posts: 4633
182
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi Stanley,

Well, ingenious with that Semaphore. Disadvantage might be the creation of a new MyOwnPausable after each fixed delay.

You could implement MyOwnPausable as, say, a Runnable, that could sleep a given time after doing its job, and that you can interrupt any time you like. You only require one Runnable and one Thread in the ThreadPool, in that case. If that makes it any better or simpler, I'm not sure.
 
Greenhorn
Posts: 15
3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What if instead of the client passing in a semaphore and acquiring/releasing that, the semaphore itself was internal and and MyOwnPausable provided its own methods for acquiring/releasing the semaphore?

Then you wouldn't have to pass anything to the constructor, though you would have to retain a reference to the MyOwnPausable instance instead.


 
Friends help you move. Good friends help you move bodies. This tiny ad will help:
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
reply
    Bookmark Topic Watch Topic
  • New Topic