三個線程輪詢數據ABC

import java.util.concurrent.CountDownLatch;
import java.util.concurrent.LinkedBlockingDeque;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

public class ThreadChange {

    public static void main(String[] args) {
        method();
    }

    private static void method() {
        CountDownLatch count = new CountDownLatch(30);

        ThreadPoolExecutor pool = new ThreadPoolExecutor(3, 3, 0, TimeUnit.SECONDS, new LinkedBlockingDeque<>(32));

        pool.execute(() -> {
            while (true) {
                if (count.getCount() == 0) {
                    break;
                }
                if (count.getCount() % 3 == 0) {
                    System.out.println("A");
                    count.countDown();
                }
            }
        });

        pool.execute(() -> {
            while (true) {
                if (count.getCount() == 0) {
                    break;
                }
                if (count.getCount() % 3 == 2) {
                    // 倒數的   所以取餘等於2的時候是B
                    System.out.println("B");
                    count.countDown();
                }
            }
        });

        pool.execute(() -> {
            while (true) {
                if (count.getCount() == 0) {
                    break;
                }
                if (count.getCount() % 3 == 1) {
                    // 倒數的   所以取餘等於1的時候是C
                    System.out.println("C");
                    count.countDown();
                }
            }
        });

    }

}
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

public class ThreadChange2 {
    private static Lock lock = new ReentrantLock();
    private static int count = 0;
    private static Condition A = lock.newCondition();
    private static Condition B = lock.newCondition();
    private static Condition C = lock.newCondition();

    public static void main(String[] args) {

        new Thread(() -> {
            lock.lock();
            try {
                for (int i = 0; i < 10; i++) {
                    while (count % 3 != 0) {
                        A.await(); //如果不滿足while條件,將本線程掛起    此時會釋放lock,將task加入到等待隊列
                    }
                    System.out.print("A");
                    count++;
                    B.signal(); // A線程執行後,喚醒下一個線程B
                }
            } catch (InterruptedException e) {
                e.printStackTrace();
            } finally {
                lock.unlock();
            }
        }).start();

        new Thread(() -> {
            lock.lock();
            try {
                for (int i = 0; i < 10; i++) {
                    while (count % 3 != 1) {
                        B.await();//如果不滿足while條件, 將本線程掛起   此時會釋放lock,將task加入到等待隊列
                    }
                    System.out.print("B");
                    count++;
                    C.signal();// B線程執行後,喚醒下一個線程C
                }
            } catch (InterruptedException e) {
                e.printStackTrace();
            } finally {
                lock.unlock();
            }
        }).start();

        new Thread(() -> {
            lock.lock();
            try {
                for (int i = 0; i < 10; i++) {
                    while (count % 3 != 2) {
                        C.await();//如果不滿足while條件, 將本線程掛起   此時會釋放lock,將task加入到等待隊列
                    }
                    System.out.println("C");
                    count++;
                    A.signal();// C線程執行後,喚醒下一個線程A
                }
            } catch (InterruptedException e) {
                e.printStackTrace();
            } finally {
                lock.unlock();
            }
        }).start();

    }

}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章