LockSupport实现线程同步

public class LockSupport01 {

    static Thread t1 = null, t2 = null;
    public static void main(String[] args) {
        char[] aI = "1234567".toCharArray();
        char[] aC = "ABCDEFG".toCharArray();
        t1 = new Thread(()->{
            for (char c : aI) {
                System.out.print(c);
                LockSupport.unpark(t2);
                LockSupport.park();
                
            }
        });
        
        t2 = new Thread(()-> {
            for (char c : aC) {
                LockSupport.park();
                System.out.print(c);
                LockSupport.unpark(t1);
                
            }
        });
        t1.start();
        t2.start();
    }
}

猜你喜欢

转载自www.cnblogs.com/wgDream/p/12038866.html
今日推荐