multithreading - Java ExecutorService:- Notify a thread to wake up when an event occurs -
i have manager class multiple threads register (used uuid
generate unique identifiers per requests), gives payload process , corresponding responses manager. using java.util.concurrent.executorservice
launch multiple threads. here implementation testing manager functionality-
public class managertest { public static void main(string[] args) { try { manager mymanager = new manager(); // start listening messages different threads mymanager.consumemessages(); int num_threads = integer.parseint(args[0]); executorservice executor = executors.newfixedthreadpool(num_threads); (int = 0; < num_threads; i++) { // class implementation given below runnable worker = new myrunnable(mymanager); executor.execute(worker); } executor.shutdown(); // wait until threads finish while (!executor.isterminated()) { } system.out.println("\nfinished threads"); mymanager.closeconnection(); } catch (ioexception | timeoutexception e) { e.printstacktrace(); } } }
here implementation of myrunnable
class
class myrunnable implements runnable { private manager managerobj; public myrunnable(manager managerobj) { this.managerobj = managerobj; } @override public void run() { try { random rand = new random(); int n = rand.nextint(35); string requestid = uuid.randomuuid().tostring(); managerobj.registerrequest(requestid, n); managerobj.publishmessage(requestid); // want avoid while loop while( ! managerobj.getrequeststatus(requestid)){ } int response = managerobj.getrequestresponse(requestid); // else managerobj.unregisterrequest(requestid); } catch (ioexception e) { e.printstacktrace(); } } }
the manager process requests , depending on payload response of request can take varying amount of time. whenever manager gets response sets requests status true calling function setrequeststatus(requestid)
. after thread exit while loop
, continues execution.
the code if working fine, thread doing work needed continuously looping on while loop until condition met.
is way make thread sleep after sending requests manager, , manager signals thread wake when response ready.
pardon me if sounds simple someone, newbie java , java-threading interface.
that's fine, used simple questions, , question written , has reasonable problem solve, see lot worse here every day, people not knowing want, how ask it, etc.
so, doing busy-spin-loop, , bad thing do, because a) consumes full cpu core per thread, , b) keeps cpu busy, means stealing processing time other threads might have useful work do.
there number of ways can solve this, list them worst best.
the simplest way improve code invoke
java.lang.thread.sleep(long millis)
method, passing0
parameter. known "yield" operation, , means "if there other threads have useful work do, let them run, , return me once done." marginally better busy-spin-looping because still consume 100% cpu. benefit consume cpu while other threads not have do, @ least not slow other things down.the next best, still not smart, way improve code invoking
java.lang.thread.sleep(long millis)
method passing1
parameter. known "pass" operation, , means "release remainder of time-slice other threads might have useful work do". in other words, remainder of time-slice forfeited, if no useful work needs done in entire system. bring cpu consumption down zero. disadvantages a) cpu consumption above zero, b) machine not able go low-power sleep mode, , c) worker thread less responsive: pick work on timeslice boundary.the best way solve problem using synchronization mechanism built java, explained in answer: https://stackoverflow.com/a/5999146/773113 not consume 0 cpu, allow machine go low power mode while waiting.
to solve problem general case, don't want wait until condition, pass information work done, use
blockingqueue
. (https://docs.oracle.com/javase/7/docs/api/java/util/concurrent/blockingqueue.html) blocking queue works using java built-in synchronization mechanism and allows 1 thread pass information another.
Comments
Post a Comment