java - Single thread with a delay to start -
what im trying : pressing 1 button change simple value. if value remains unchanged 3 sec, wanna execute method. need single thread start(and kill others) on every button click. know
executor executor = executors.newsinglethreadscheduledexecutor();
but can't have delay. best practice ?
either can go simple thread
start-stop/interrupt thing or can use api's scheduledexecutorservice
.
example of how scheduledexecutorservice
below
//start many threads want upon button click... (since said want kill other threads, assume want start threads upon button click) scheduledexecutorservice service = executors.newscheduledthreadpool(3); service.schedule(new thread(), 0, timeunit.seconds); service.schedule(new thread(), 0, timeunit.seconds); service.schedule(new thread(), 0, timeunit.seconds); //start single thread after 3 seconds. scheduledexecutorservice servicecheck = executors.newscheduledthreadpool(3); servicecheck.schedule(new thread(), 3, timeunit.seconds); //in thread created using "servicecheck", check value has not changed use `service.shutdownnow();` shutdown started threads , whatever want.
Comments
Post a Comment