Java Multithreading (v) mode-guarded suspension

Source: Internet
Author: User

  • Guarded suspension
  • Multithreading shares a resource, and the use of that resource is conditional.

    • Applicable environment
      • The occupancy of a shared resource is conditional rather than directly occupied.

    • Sample Example
      • Early Childhood Education collection: Three bear children preschool education, two preschool education before a long table, a total of only two homework.

    • Bear Boy
    • Package Guardedsuspension;public class Child implements Runnable {private Table table=null;private String myname=null; private int Cnt=1;public Child (Table table,string myName) {this.table=table;this.myname=myname;} @Overridepublic void Run () {while (cnt<=20) {Assignment a=new Assignment (this.myname,this.myname+ "_" +cnt); System.out.println (this.myname+ "+cnt"); Table.put (a); cnt++;try {thread.sleep (100);} catch (Interruptedexception e) {}}}}
    • Early childhood education
    • Package Guardedsuspension;public class Teacher implements Runnable{private final String myname;private final table table; private int state=0;public Teacher (table table, String name) {this.myname=name;this.table=table;} public void setState (int state) {this.state=state;} @Overridepublic void Run () {while (state==0) {assignment a=table.get (); if (a!=null) {A.setresult (MyName, "Y"); A.showresult ();}}}
    • Table
    •  package guardedsuspension;import java.util.linkedlist;//guarded objectpublic  class table {private final linkedlist<assignment> table=new linkedlist< Assignment> ();p rivate final int size;private int state=0;public table (int  size) {this.size=size;} Guarded methodpublic synchronized void put (assignment assignment) {while (Table.size ( ) {>=size) {try {wait ();}  catch  (interruptedexception e)  {}}table.add (Assignment); Notifyall ();} State change methodpublic synchronized assignment get () {Assignment ret=null; while (Table.size () <=0&&state==0) {try {wait ();//wait,  //if the table state is not last woken up in the main thread, it needs to be timed. } catch  (interruptedexception e)  {}}ret=table.poll (); Notifyall (); return ret;} Public void setstate (int state) {this.state=state;}} 
    • Homework
    • Package Guardedsuspension;public class Assignment {Private final string childname;private final string assignmentname; Private String Teachername= "N/A";p rivate string result= "N/a";p ublic assignment (String childname,string assignmentname ) {this.childname=childname;this.assignmentname=assignmentname;} public void Setresult (String teachername,string result) {This.teachername=teachername;this.result=result;} public void Showresult () {System.out.println ("Child:" +childname+ "to Teacher:" +teachername+ "job:" +assignmentname+ "to correct the result:" + result);}}
    • Test class
    • Package guardedsuspension;public class test {public static void main (String [] args]  {table table=new table (2); Teacher t1=new teacher (table, "T1"); Teacher t2=new teacher (table, "T2"); Child c1=new child (table, "C1"); Child c2=new child (table, "C2"); Child c3=new child (table, "C3"); Thread tchild1=new thread (C1); Thread tchild2=new thread (C2); Thread tchild3=new thread (C3); Thread tteacher1=new thread (t1); Thread tteacher2=new thread (T2); Tchild1.start (); Tchild2.start (); Tchild3.start (); TTeacher1.start (); Tteacher2.start ();//wait for the children to complete all locks also try {tchild1.join (); Tchild2.join (); Tchild3.join ();}  catch  (interruptedexception e)  {}//If wait is timed in the table class, you can set the table state Table.setstate (-1) directly; to inform. Synchronized (table) {System.out.println ("All children have left");//Set teacher State t1.setstate ( -1); T2.setstate (-1);//Get the table lock, change its state , and notify all who are waiting at the table to leave. Table.setstate ( -1); tablE.notifyall ();}}} 

This article is from the Java Technology Stack notes blog, so be sure to keep this source http://stroll.blog.51cto.com/11038467/1855761

Java Multithreading (v) mode-guarded suspension

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.