爲兼容jdk1.5之前版本的信號量操作:
1.Semahpore
2
package com.jzy.thread; import java.util.Random; import java.util.Vector; //信號量 public class Semahpore { private Vector locks=new Vector(); //保存線程 private int permitNum=1; //允許的的信號量數 private int nowPermitNum=1; //現在的信號量數 private boolean isAllowGrow=false; //是否自動增長信號量數 private boolean fair=false; //是否公平 Random random=new Random(); public Semahpore(int permitNum,boolean isAllowGrow,boolean fair){ this.permitNum=permitNum; nowPermitNum=permitNum; this.isAllowGrow=isAllowGrow; this.fair=fair; } public Semahpore(int permitNum,boolean fair){ this(permitNum, true, fair); } public Semahpore(int permitNum){ this(permitNum, false); } // 消費者希望從信號量中獲取一個信號許可,如果目前仍然存在可用許可, //則消費者順利執行,否則消費者線程阻塞等待,直到生產者生產資源,產生新的許可 public void acquier(){ if(nowPermitNum>0){ nowPermitNum--; }else{ Object obj=new Object(); locks.add(obj); synchronized (obj) { try { obj.wait(); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } } // 生產者已經生產了新的可供消費資源,向信號量中增加一個可用許可 public void relase(){ if(locks.size()>0){ int index=0; if(!fair){ index=Math.abs(random.nextInt())%locks.size(); } Object obj=locks.get(index); locks.remove(index); synchronized (obj) { obj.notify(); } }else if(isAllowGrow||nowPermitNum<permitNum){ nowPermitNum++; } } }
2.TestCustomerAndPudction
package com.jzy.thread; import java.util.Random; import java.util.UUID; import java.util.Vector; public class TestCustomerAndPudction { /** * @param args */ public static void main(String[] args) { final Vector buffer = new Vector(); //保存生產者產生的數據 final Random random = new Random(); final Semahpore sem = new Semahpore(0,true,true); final int x = Math.abs(random.nextInt()) % 100; //定義消費者等待時間 final int y = Math.abs(random.nextInt()) % 1000; //定義生產者等待時間 Thread c1 = new Thread() { public void run() { while (true) { System.out.println("消費者1請求獲取資源"); sem.acquier(); String str = buffer.get(0).toString(); System.out.println("消費者1獲取資源是" + str); try { Thread.sleep(100 + x); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } }; Thread c2 = new Thread() { public void run() { while (true) { System.out.println("消費者2請求獲取資源"); sem.acquier(); String str = buffer.get(0).toString(); System.out.println("消費者2獲取資源是" + str); try { Thread.sleep(100 + x); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } }; Thread c3 = new Thread() { public void run() { while (true) { System.out.println("消費者3請求獲取資源"); sem.acquier(); String str = buffer.get(0).toString(); System.out.println("消費者3獲取資源是" + str); try { Thread.sleep(100 + x); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } }; c1.start(); c2.start(); c3.start(); Thread p1 = new Thread() { public void run() { while (true) { try { Thread.sleep(1000 + y); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } String str=UUID.randomUUID().toString(); buffer.add(str); System.out.println("生產者1生產了資源:"+str); sem.relase(); } } }; Thread p2 = new Thread() { public void run() { while (true) { try { Thread.sleep(1000 + y); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } String str=UUID.randomUUID().toString(); buffer.add(str); System.out.println("生產者2生產了資源:"+str); sem.relase(); } } }; p1.start(); p2.start(); } }