• 多线程打印数字


    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:	" + threadID + "	:	" + 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:	" + threadID + "	:	" + 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();
                    }
                }
            }
        }
    }
    
    
  • 相关阅读:
    html +JS 自学
    Linux下SVN多版本库管理
    Jenkins更换国内源
    Kubernetes Service
    Kubernetes Pod
    ubuntu下vim配置日常工作版本
    PYTHON替代MATLAB在线性代数学习中的应用(使用Python辅助MIT 18.06 Linear Algebra学习)
    mongodb 片键需要思考的问题
    SpringBoot--Easycode插件自定义模板
    Docker-概述
  • 原文地址:https://www.cnblogs.com/liulaolaiu/p/11744372.html
Copyright © 2020-2023  润新知