多線程打印數字

package learning_java.Exercise;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

public class MultiThreadPrinter {
    public static void main(String[] args) {
        int gap = 3;
        int end = 100;
        ExecutorService executor = Executors.newFixedThreadPool(gap);
        for (int i = 0; i < gap; i ++) {
            executor.execute(new PrinterWithLock(i, gap, end));
        }
        executor.shutdown();

    }

    private static class Printer implements Runnable{
        private static final Object LOCK = new Object();

        private int threadID;
        private final int GAP;
        private static int count = 0;
        private Printer(int id, int gap) {
            threadID = id;
            GAP = gap;
        }

        @Override
        public void run() {
            while (true) {
                synchronized (LOCK) {
                    while (count % GAP != this.threadID) {
                        if (count >= 101) break;
                        try {
                            LOCK.wait();
                        } catch (InterruptedException ex) {
                            ex.printStackTrace();
                        }
                    }
                    if (count >= 101) break;
                    System.out.println("ThreadID:\t" + threadID + "\t:\t" + count);
                    ++count;
                    LOCK.notifyAll();
                }
            }
        }
    }

    private static class PrinterWithLock implements Runnable {
        // 需要注意,這裏需要都爲private static
        private static Lock lock = new ReentrantLock();
        // Condition若不是static的,則無法傳遞消息
        private static Condition isCurrentID = lock.newCondition();
        private final int threadID;
        private final int GAP;
        private static int count = 0;
        private final int END;

        private PrinterWithLock(int id, int gap, int end) {
            threadID = id;
            GAP = gap;
            END = end;
        }

        @Override
        public void run() {
            while (true) {
                try {
                    lock.lock();
                    while (count % GAP != threadID) {
                        if (count > END) break;
                        isCurrentID.await();
                    }
                    if (count > END) break;
                    System.out.println("ThreadID:\t" + threadID + "\t:\t" + count);
                    ++count;
                    isCurrentID.signalAll();
                } catch (InterruptedException ex) {
                    ex.printStackTrace();
                } finally {
                    lock.unlock();
                }
            }
        }


//        @Override
//        public void run() {
//            while (true) {
//                try {
//                    lock.lock();
//                    while (count % 3 != this.threadID) {
//                        if (count >= 101) {
//                            break;
//                        }
//                        try {
//                            isCurrentID.await();
//                        } catch (InterruptedException e) {
//                            e.printStackTrace();
//                        }
//                    }
//
//                    if (count >= 101) {
//                        break;
//                    }
//                    System.out.println("thread-" + this.threadID + ":" + count);
//                    count++;
//
//                    isCurrentID.signalAll();
//
//                } finally {
//                    lock.unlock();
//                }
//            }
//        }
    }

    private static class PrintThread2 implements Runnable {
        private static final ReentrantLock lock = new ReentrantLock();
        private static final Condition c = lock.newCondition();

        private static int count = 0; //作爲計數,同時也作爲資源;因爲這道題目是自然數作爲資源,所以正好可以公用;
        private Integer threadNo;

        public PrintThread2(Integer threadNo) {
            this.threadNo = threadNo;
        }

        @Override
        public void run() {
            while (true) {
                try {
                    lock.lock();
                    while (count % 3 != this.threadNo) {
                        if (count >= 101) {
                            break;
                        }
                        try {
                            c.await();
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }

                    if (count >= 101) {
                        break;
                    }
                    System.out.println("thread-" + this.threadNo + ":" + count);
                    count++;

                    c.signalAll();

                } finally {
                    lock.unlock();
                }
            }
        }
    }
}

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