多线程中使用Lock和Condition机制

来源:互联网 发布:instanceof php 编辑:程序博客网 时间:2024/06/07 06:22

1、资源类

package cn.toltech.t_concurrent;import java.util.ArrayList;import java.util.List;import java.util.concurrent.locks.Condition;import java.util.concurrent.locks.Lock;import java.util.concurrent.locks.ReentrantLock;/** * Created by sz0816 on 15-1-2 */public class CakeBox {    private List<Integer> list = new ArrayList<Integer>();    private final int MAX_SIZE = 1;    private Lock lock = new ReentrantLock();    private Condition takeCondition = lock.newCondition();    private Condition putCondition = lock.newCondition();    /****     * 使用Condition来进行控制多线程的锁的问题     * @return     */    public Integer getCake1(){        Integer result = null;        lock.lock();        try{            while(list.isEmpty()){                try {                    System.out.println("take wait...");                    takeCondition.await();                } catch (InterruptedException e) {                    e.printStackTrace();                }            }            result = list.remove(0);            System.out.println("get..."+result);            putCondition.signal();        }finally {            lock.unlock();        }        return result;    }    /****     * 使用Condition来进行控制多线程的锁的问题     * @return     */    public void putCake1(Integer integer){        lock.lock();        try{            while(list.size()>=MAX_SIZE){                try {                    System.out.println("put wait...");                    putCondition.await();                } catch (InterruptedException e) {                    e.printStackTrace();                }            }            System.out.println("add..."+integer);            list.add(integer);            takeCondition.signal();        }finally {            lock.unlock();        }    }    /***普通方式**/    public synchronized Integer getCake(){        while(list.isEmpty()){            try {                System.out.println("take wait...");                wait();            } catch (InterruptedException e) {                e.printStackTrace();            }        }        Integer integer = list.remove(0);        System.out.println("get..."+integer);        notifyAll();        return integer;    }    public synchronized void putCake(Integer integer){        while(list.size()>=MAX_SIZE){            try {                System.out.println("put wait...");                wait();            } catch (InterruptedException e) {                e.printStackTrace();            }        }        System.out.println("add..."+integer);        list.add(integer);        notifyAll();    }}

2、取资源

package cn.toltech.t_concurrent;import java.util.List;/** * Created by sz0816 on 15-1-21. */public class GetCake implements Runnable{    public CakeBox cakeBox;    public GetCake(CakeBox cakeBox){        this.cakeBox = cakeBox;    }    public Integer getCake(){        return cakeBox.getCake1();    }    @Override    public void run() {        while(true){            getCake();            try {                Thread.sleep(10);            } catch (InterruptedException e) {                e.printStackTrace();            }        }    }}


3、放资源

package cn.toltech.t_concurrent;/** * Created by sz0816 on 15-1-21. */public class PutCake implements Runnable{    private CakeBox cakeBox;    private static int cakeNum;    public PutCake(CakeBox cakeBox){        this.cakeBox = cakeBox;    }    public void putCake(){        cakeBox.putCake1(cakeNum++);    }    @Override    public void run() {        while(true){            putCake();            try {                Thread.sleep(2000);            } catch (InterruptedException e) {                e.printStackTrace();            }        }    }}


4、测试主程序

package cn.toltech.t_concurrent;/** * Created by sz0816 on 15-1-21. */public class TestReentrantLock {    public static void main(String []args) throws Exception{        CakeBox cakeBox = new CakeBox();        new Thread(new GetCake(cakeBox)).start();        new Thread(new PutCake(cakeBox)).start();        new Thread(new PutCake(cakeBox)).start();        while(true){            Thread.sleep(1000);        }    }}


0 0
原创粉丝点击