1. 程式人生 > 實用技巧 >使用synchronized,wait,notifyAll 實現兩個執行緒交替列印

使用synchronized,wait,notifyAll 實現兩個執行緒交替列印

package customer;

/**
 * @Author lizhilong
 * @create 2020/7/6 22:22
 * @desc
 */
public class ExchangePrint {
    public static void main(String[] args) {


        Object object = new Object();
        new Thread(()->{
            char a = 'A';
            synchronized (object){
                for(int
i=0;i<26;i++){ System.out.println(a); try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } a++; object.notifyAll();
try { object.wait(); } catch (InterruptedException e) { e.printStackTrace(); } } } }).start(); try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); }
new Thread(()->{ synchronized (object){ for(int i=1;i<27;i++){ System.out.println(i); try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } object.notifyAll(); try { object.wait(); } catch (InterruptedException e) { e.printStackTrace(); } } } }).start(); } }

執行結果: