兩個(gè)線程交替打印奇偶數(shù)

序言

以前看過(guò)多線程交替打印奇偶數(shù),知道大概怎么寫,實(shí)際寫的時(shí)候會(huì)卡住,特此記錄下來(lái)

方法一:wait, notify,性能較差,不推薦使用

public class TestThread {
    public static int i = 1;

    public static final int TOTAL = 100;

    public static Object lock = new Object();

    public static void main(String[] args) {
        Thread thread1 = new Thread(() -> {
            while (i <= TOTAL) {
                synchronized (lock) {
                    if (i % 2 == 1) {
                        System.out.println("i=" + i++);
                        lock.notify();
                    } else {
                        try {
                            lock.wait();
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                }
            }
        });

        Thread thread2 = new Thread(() -> {
            while (i <= TOTAL) {
                synchronized (lock) {
                    if (i % 2 == 0) {
                        System.out.println("i=" + i++);
                        lock.notify();
                    } else {
                        try {
                            lock.wait();
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                }
            }
        });

        thread1.start();
        thread2.start();
    }
}

方法二:countDownLatch,推薦使用

public class TestThread2 {
    private static AtomicInteger num = new AtomicInteger(1);

    private static CountDownLatch countDownLatch = new CountDownLatch(2);

    public static void main(String[] args) throws InterruptedException {
        Thread t1 = new Thread() {
            @Override
            public void run() {
                while (num.intValue() < 100) {
                    if (num.intValue() % 2 == 1) {
                        System.out.println("奇數(shù)線程:"+num.intValue());
                        num.incrementAndGet();
                    }
                    countDownLatch.countDown();
                }
            }
        };

        Thread t2 = new Thread() {
            @Override
            public void run() {
                while (num.intValue() <= 100) {
                    if (num.intValue() % 2 == 0) {
                        System.out.println("偶數(shù)線程:"+num.intValue());
                        num.incrementAndGet();
                    }
                    countDownLatch.countDown();
                }
            }
        };

        t1.start();
        t2.start();
        countDownLatch.await();
    }
}
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 引言 java面試中經(jīng)常會(huì)遇到這個(gè)問(wèn)題,如何用兩個(gè)線程交替打印奇偶數(shù)。線程A打印1,線程B打印2,線程A打印3,線...
    someoneYoung閱讀 7,684評(píng)論 0 9
  • 多線程輸出結(jié)果是不可控的,需要實(shí)現(xiàn)的結(jié)果是使用兩個(gè)線程交替打印奇偶數(shù) 1.兩個(gè)線程同時(shí)操作一個(gè)變量如: _numb...
    某非著名程序員閱讀 3,547評(píng)論 0 6
  • 多線程交替打印1~10的奇偶數(shù) 思路 搞兩條線程,一條線程打印奇數(shù)任務(wù),一條線程打印偶數(shù)任務(wù)。 為了防止線程間的無(wú)...
    sunpy閱讀 5,336評(píng)論 0 2
  • 相關(guān)概念 面向?qū)ο蟮娜齻€(gè)特征 封裝,繼承,多態(tài).這個(gè)應(yīng)該是人人皆知.有時(shí)候也會(huì)加上抽象. 多態(tài)的好處 允許不同類對(duì)...
    東經(jīng)315度閱讀 2,212評(píng)論 0 8
  • 渡小滿6閱讀 375評(píng)論 0 1

友情鏈接更多精彩內(nèi)容