| The worker thread mode is like Producer consumer Mode The request behavior is like Command mode . The producer consumer mode focuses on the production and consumption of the product. It does not discuss how the product is consumed. If your product is a request, the consumer executes the request method specified in the request after obtaining the request, that is, the command mode is used, and your Request Buffer also manages the consumer, it means the worker thread mode. On the sequence divisor, we can see that the worker thread shows both the producer consumer mode and the command mode: A channel class implemented using Java is as follows:
Import java. util. Collections list; Public class channel { Private Response List requests; Private workerthread [] workerthreads; Public channel (INT threadnumber ){ Requests = new queue list (); Workerthreads = new workerthread [threadnumber]; For (INT I = 0; I <workerthreads. Size (); I ++ ){ Workerthreads= New workerthread (); Workerthreads. Start (); } } Public synchronized void putrequest (request) { while (requests. size ()> = 2) {// Capacity Limit: 2 try { wait (); }< br> catch (interruptedexception E) {}< BR >} Requests. addlast (request ); Policyall (); }
Public synchronized request getproduct (){ While (requests. Size () <= 0 ){ Try { Wait (); } Catch (interruptedexception e ){} } Request request = (request) requests. removefirst (); Policyall ();
Return request; } } Command mode used between the request class and workerthread class:
Public class request (){ //.... Public void execute (){ // Do some work .... } }
Public class workerthread extends thread { //... Public void run (){ While (true ){ Request request = channel. getrequest (); Request.exe cute (); } } } in terms of behavior, workerthread performs a request. If no request is sent, all workerthreads will wait, wait until new jobs come in and notify them of the work to be done by the workerthread that gets the request, which is directly defined in execute. |