用Lock Condition实现线程通信


编写两个线程,一个线程打印1~25,另一个线程打印字母A~Z,打印顺序为12A34B56C……5152Z,要求使用线程间的通信

package thread.notify;

import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
/**
 * @ClassName: LockCondition
 * @Description: 通过Lock 和 Condition实现线程通信
 * @author Administrator
 * @date 2018-7-8 上午10:44:27
 */
public class LockCondition {
    private Lock lock = new ReentrantLock(true);
    private Condition numShow = lock.newCondition();
    private Condition strShow = lock.newCondition();
    private volatile int sign = 1;//1打印数字;2打印字符串
    
    public Runnable numThread() {
        final String[] inputArr = buildNumArr(52);
        return new Runnable() {
            private String[] arr = inputArr;
            public void run() {
                for (int i = 0; i < arr.length; i=i+2) {
                    try {
                        lock.lock();
                        while(sign == 2){
                        	numShow.await();
                        }
                        System.out.print(arr[i]+""+arr[i + 1]);
                        sign = 2;
                        strShow.signalAll();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    } finally {
                        lock.unlock();
                    }
                }
            }
        };
    }
    public Runnable strThread() {
        final String[] inputArr = buildStrArr(26);
        return new Runnable() {
            private String[] arr = inputArr;
            public void run() {
                for (int i = 0; i < arr.length; i++) {
                    try {
                        lock.lock();
                        while(sign == 1){
                        	strShow.await();
                        }
                        System.out.print(arr[i]);
                        sign = 1;
                        numShow.signalAll();
                    } catch (Exception e) {
                        e.printStackTrace();
                    } finally {
                        lock.unlock();
                    }
                }
            }
        };
    }
    public static void main(String args[]) throws InterruptedException {
        LockCondition two = new LockCondition();
        new Thread(two.numThread()).start();
        new Thread(two.strThread()).start();
    }
    
    public static String[] buildNumArr(int max) {
        String[] noArr = new String[max];
        for(int i=0;i<max;i++){
            noArr[i] = Integer.toString(i+1);
        }
        return noArr;
    }
 
    public static String[] buildStrArr(int max) {
        String[] charArr = new String[max];
        int tmp = 65;
        for(int i=0;i<max;i++){
            charArr[i] = String.valueOf((char)(tmp+i));
        }
        return charArr;
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值