• 多线程打印数字


    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();
                    }
                }
            }
        }
    }
    
    
  • 相关阅读:
    Oracle 查询语句截取字符串
    删除ORACLE数据库中重复的数据
    [易学C#]C#3.0语言新特性之匿名类型
    [讨论]程序之路在何方?
    [易学C#]C#3.0语言新特性之扩展方法
    [易学C#]C#3.0语言新特性之对象和集合初始化器
    用C#解决Oracle9i和Oracle10g字符集不兼容的问题
    一个C#操作Oracle的通用类
    [易学C#]C#3.0语言新特性之隐式类型
    重拾 DirectX 一:配置Visual Studio 2008+Microsoft DirectX SDK (June 2008) 开发环境
  • 原文地址:https://www.cnblogs.com/liulaolaiu/p/11744372.html
Copyright © 2020-2023  润新知