多线程中使用Lock和Condition机制

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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值