JAVA多线程模式-Guarded Suspension
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了JAVA多线程模式-Guarded Suspension相关的知识,希望对你有一定的参考价值。
Guarded Suspension
多线程共享一个资源,该资源的使用是有条件的。
适用环境
共享资源的占用是有条件而非直接占用的。
样例
幼教收作业:仨熊孩子俩幼教,俩幼教面前一个长桌子,总共只能放两个作业。
熊孩子
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; } @Override public 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) { } } } }
幼教
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; } @Override public void run() { while(state==0){ Assignment a=table.get(); if(a!=null){ a.setResult(myName, "Y"); a.showResult(); } } } }
桌子
package GuardedSuspension; import java.util.LinkedList; //guarded object public class Table { private final LinkedList<Assignment> table=new LinkedList<Assignment>(); private final int size; private int state=0; public Table(int size){ this.size=size; } //guarded method public synchronized void put(Assignment assignment){ while(table.size()>=size){ try { wait(); } catch (InterruptedException e) { } } table.add(assignment); notifyAll(); } //state change method public synchronized Assignment get(){ Assignment ret=null; while(table.size()<=0&&state==0){ try { wait(); //wait(100); //如果并未在主线程对桌子状态进行最后的唤醒,需要是定时等待。 } catch (InterruptedException e) { } } ret=table.poll(); notifyAll(); return ret; } public void setState(int state){ this.state=state; } }
作业
package GuardedSuspension; public class Assignment { private final String childName; private final String assignmentName; private String teacherName="N/A"; private String result="N/A"; public 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("孩子:"+childName+"交给老师:"+teacherName+"的作业:"+assignmentName+"批改结果:"+result); } }
测试类
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(); //等待孩子们完成所有锁也 try { tChild1.join(); tChild2.join(); tChild3.join(); } catch (InterruptedException e) { } //如果在table类中,如果wait是定时的,则可以通过直接设置桌子状态table.setState(-1);来通知。 synchronized(table){ System.out.println("所有孩子已经离开了"); //设置老师状态 t1.setState(-1); t2.setState(-1); //获取table的锁后,变更其状态,同时通知所有等待在桌子旁的人离开。 table.setState(-1); table.notifyAll(); } } }
本文出自 “JAVA技术栈笔记” 博客,请务必保留此出处http://stroll.blog.51cto.com/11038467/1855761
以上是关于JAVA多线程模式-Guarded Suspension的主要内容,如果未能解决你的问题,请参考以下文章
实战并发编程 - 09多线程Guarded Suspension模式案例实战
多线程编程-设计模式之保护性暂挂(Guarded Suspesion)模式