Multi-threaded Series four: Guarded suspension mode

Source: Internet
Author: User

One, what is guarded suspension mode
If performing the current processing can cause problems, let the thread that executes the processing wait. This mode guarantees the security of the instance by allowing the thread to wait

Second, an example of a simple inter-threading communication is implemented

One thread (clientthread) passes an instance of the request (requests) to another thread (Serverthread)

Request: Thread Instance

Requestqueue: A queue that holds instances of requests (request)

Clientthread: Putting a thread instance in the queue

Serverthread: Fetching threads from a queue example

Sample Program

 Public class Request {    privatefinal  String name;      Public Request (String name) {        this. Name = name;    }       Public String GetName () {        return  name;    }    @Override    public  String toString () {        return "request{" +                " Name= ' + name + ' \ ' +                '} ';}    }

 Public classRequestqueue {Privatequeue<request> queue =NewLinkedlist<>();  Public synchronized voidputrequest (Request request) {Queue.offer (request);    Notifyall (); }     Public synchronizedRequest getrequest () { while(Queue.peek () = =NULL){            Try{wait (); } Catch(interruptedexception e) {e.printstacktrace (); }        }        returnQueue.remove (); }}
 Public classClientthreadextendsThread {Private Finalrandom random; Private FinalRequestqueue Requestqueue;  PublicClientthread (requestqueue requestqueue,string name,Longseed) {        Super(name);  This. Random =NewRandom (SEED);  This. Requestqueue =Requestqueue; } @Override Public voidrun () { for(inti = 0; I < 10000; i++) {Request Request=NewRequest ("N0.") +i); System.out.println (Thread.CurrentThread (). GetName ()+ "Requests" +request);            Requestqueue.putrequest (Request); Try{thread.sleep (Random.nextint (1000)); } Catch(interruptedexception e) {e.printstacktrace (); }        }    }}
 Public classServerthreadextendsthread{Private Finalrandom random; Private FinalRequestqueue Requestqueue;  PublicServerthread (requestqueue requestqueue,string name,Longseed) {        Super(name);  This. Random =NewRandom (SEED);  This. Requestqueue =Requestqueue; } @Override Public voidrun () { for(inti = 0; i < 1000; i++) {Request Request=requestqueue.getrequest (); System.out.println (Thread.CurrentThread (). GetName ()+ "handles" +request); Try{thread.sleep (Random.nextint (1000)); } Catch(interruptedexception e) {e.printstacktrace (); }        }    }}
 Public class Test {    publicstaticvoid  main (string[] args) {        new requestqueue ();         New Clientthread (Requestqueue, "Alice", 3141592L). Start ();         New Serverthread (Requestqueue, "Bobby", 6583184L). Start ();    }}

Third, guarded role in the suspension model

Guardedobject: The object being guarded
Guardedobject is a class that holds the guarded method (Guardedmethod), and when the thread executes the Guardedmethod method, it can be executed immediately if the daemon is established.
When the Guardian condition is not established, it is necessary to wait.
The setting of the Guardian condition is related to the state of the Daemon object.
So in the example program above
Requestqueue: is the Guardian object
Getrequest method: Is the Guardian method
Putrequest method: How to change the state

Four, wait and Notify/notifyall's responsibility

In the example program above, we wrote the Wait/notifyall in the Requestqueue class and did not appear in the Clientthread,serverthread,main class.
The implementation of the guarded suspension pattern is encapsulated in the Requestqueue class.
This practice of hiding wait/notifyall is very important for the reusability of the Requestqueue class. When we use Requestqueue, other classes do not have to consider wait,notifyall problems,
Just call the Getrequest method or the Putrequst method on the line.

Five, using the Java.util.concurrent.LinkedBlockingQueue sample program

This class and the Requestqueue class function the same. The Take method in this class is used to remove the first element, and the put method is used to add elements to the end of the queue. When the queue is empty, if the take method is called, wait is
And take and put have considered mutex processing. So the getrequest and Putrequest methods need not be declared as synchronized. The guarded suspension mode is used in the Linkedblockingqueue class.

Code:

 Public classRequestqueue {Private Finalblockingqueue<request> queue =NewLinkedblockingqueue<>();  Public voidputrequest (Request request) {Try{queue.put (request); } Catch(interruptedexception e) {e.printstacktrace (); }    }     PublicRequest Getrequest () {Request Request=NULL; Try{Request=Queue.take (); } Catch(interruptedexception e) {e.printstacktrace (); }        returnrequest; }}

Multi-threaded Series four: Guarded suspension mode

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.