【高并发面试题】要求用两个线程顺序打印A1B2C3...Z26

方法一、LockSupport

public class TestD3 {

    static Thread t1 = null;
    static Thread t2 = null;

    public static void main(String[] args) {
        TestD3 t = new TestD3();
        String[] strs = new String[]{"A","B","C","D","E","F","G","H",
        "I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z"
        };

        t1 = new Thread(() -> {
            for (String str :strs) {
                System.out.print(str);
                LockSupport.unpark(t2);
                LockSupport.park();
            }
        },"T1");

        t2 = new Thread(() -> {
            for (int i = 1; i <= 26; i++) {
                LockSupport.park();
                System.out.print(i);
                LockSupport.unpark(t1);
            }
        },"T2");

        t1.start();
        t2.start();

    }
}

方法二、wait()和notify()   

        有瑕疵,无法确保t1在t2之前执行,可能会出现1A2B...26Z

public class TestD1 {

    public static void main(String[] args) {
        final Object o = new Object();
        String[] strs = new String[]{"A","B","C","D","E","F","G","H",
        "I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z"
        };
        Thread t1 = new Thread(() -> {
            synchronized (o) {
                for (String str :strs) {
                    System.out.print(str);
                    try {
                        o.notify();//唤醒T2
                        o.wait();//T1释放锁,T2执行
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    } finally {
                        o.notify();
                    }
                }
            }
        },"T1");

        Thread t2 = new Thread(() -> {
            synchronized (o) {
                try {
                    o.wait();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                for (int i = 1; i <= 26; i++) {
                    System.out.print(i);
                    o.notify();//唤醒T1
                    if (i<26){
                        try {
                            o.wait();//释放锁,让T1执行
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                }
            }
        },"T2");

        t1.start();
        t2.start();

    }
}

方法三、ReentrantLock

        有瑕疵,无法确保t1在t2之前执行,可能会出现1A2B...26Z

public class TestD2 {

    public static void main(String[] args) {
        Lock lock = new ReentrantLock();
        Condition lockT1 =  lock.newCondition();
        Condition lockT2 =  lock.newCondition();

        String[] strs = new String[]{"A","B","C","D","E","F","G","H",
        "I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z"
        };

        Thread t1 = new Thread(() -> {
            lock.lock();
            try {
                for (String str :strs) {
                    System.out.print(str);
                        lockT2.signal();
                        lockT1.await();

                }
            } catch (InterruptedException e) {
                e.printStackTrace();
            } finally {
              lock.unlock();
            }
        },"T1");

        Thread t2 = new Thread(() -> {
            lock.lock();
            try {
                for (int i = 1; i <= 26; i++) {
                    System.out.print(i);
                    lockT1.signal();
                    if (i<26){
                        lockT2.await();
                    }
                }
            } catch (InterruptedException e) {
                e.printStackTrace();
            } finally {
                lock.unlock();
            }
        },"T2");
        
        t1.start();
        t2.start();

    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值