• Java并发面试题:三个线程轮流打印十次abc


    方法1:用while循环和变量实现

        static int index = 1;
    
        public static void main(String[] args) {
            Thread t1 = new Thread(new Runnable() {
                @Override
                public void run() {
                    while (true) {
                        if (index > 30) {
                            break;
                        }
                        if (index % 3 == 1) {
                            System.out.println("a");
                            index++;
                        }
                    }
                }
            });
            Thread t2 = new Thread(new Runnable() {
                @Override
                public void run() {
                    while (true) {
                        if (index > 30) {
                            break;
                        }
                        if (index % 3 == 2) {
                            System.out.println("b");
                            index++;
                        }
                    }
                }
            });
            Thread t3 = new Thread(new Runnable() {
                @Override
                public void run() {
                    while (true) {
                        if (index > 30) {
                            break;
                        }
                        if (index % 3 == 0) {
                            System.out.println("c");
                            index++;
                        }
                    }
                }
            });
            t1.start();
            t2.start();
            t3.start();
        }
    

    方法2:用synchronized、wait、notifyAll实现

        static int index = 1;
    
        public static void main(String[] args) throws InterruptedException {
            final Object lock = new Object();
            Thread t1 = new Thread(new Runnable() {
                @Override
                public void run() {
                    synchronized (lock) {
                        for (int i = 0; i < 10; i++) {
                            while (index % 3 != 1) {
                                try {
                                    lock.wait();
                                } catch (InterruptedException e) {
                                    e.printStackTrace();
                                }
                            }
                            System.out.println("a");
                            index++;
                            lock.notifyAll();
                        }
                    }
                }
            });
            Thread t2 = new Thread(new Runnable() {
                @Override
                public void run() {
                    synchronized (lock) {
                        for (int i = 0; i < 10; i++) {
                            while (index % 3 != 2) {
                                try {
                                    lock.wait();
                                } catch (InterruptedException e) {
                                    e.printStackTrace();
                                }
                            }
                            System.out.println("b");
                            index++;
                            lock.notifyAll();
                        }
                    }
                }
            });
            Thread t3 = new Thread(new Runnable() {
                @Override
                public void run() {
                    synchronized (lock) {
                        for (int i = 0; i < 10; i++) {
                            while (index % 3 != 0) {
                                try {
                                    lock.wait();
                                } catch (InterruptedException e) {
                                    e.printStackTrace();
                                }
                            }
                            System.out.println("c");
                            index++;
                            lock.notifyAll();
                        }
                    }
                }
            });
            t1.start();
            t2.start();
            t3.start();
        }
    

    方法3:用ReentrantLock、1个Condition实现

        static int index = 1;
    
        public static void main(String[] args) {
            final ReentrantLock lock = new ReentrantLock();
            final Condition condition = lock.newCondition();
            Thread t1 = new Thread(new Runnable() {
                @Override
                public void run() {
                    lock.lock();
                    try {
                        for (int i = 0; i < 10; i++) {
                            while (index % 3 != 1) {
                                condition.await();
                            }
                            System.out.println("a");
                            index++;
                            condition.signalAll();
                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                    } finally {
                        lock.unlock();
                    }
                }
            });
            Thread t2 = new Thread(new Runnable() {
                @Override
                public void run() {
                    lock.lock();
                    try {
                        for (int i = 0; i < 10; i++) {
                            while (index % 3 != 2) {
                                condition.await();
                            }
                            System.out.println("b");
                            index++;
                            condition.signalAll();
                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                    } finally {
                        lock.unlock();
                    }
                }
            });
            Thread t3 = new Thread(new Runnable() {
                @Override
                public void run() {
                    lock.lock();
                    try {
                        for (int i = 0; i < 10; i++) {
                            while (index % 3 != 0) {
                                condition.await();
                            }
                            System.out.println("c");
                            index++;
                            condition.signalAll();
                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                    } finally {
                        lock.unlock();
                    }
                }
            });
            t1.start();
            t2.start();
            t3.start();
        }
    

    方法4:用ReentrantLock、三个Condition实现

        public static void main(String[] args) {
            final Lock lock = new ReentrantLock();
            final Condition condition1 = lock.newCondition();
            final Condition condition2 = lock.newCondition();
            final Condition condition3 = lock.newCondition();
            Thread t1 = new Thread(new Runnable() {
                @Override
                public void run() {
                    lock.lock();
                    try {
                        for (int i = 0; i < 10; i++) {
                            condition1.await();
                            System.out.println("a");
                            condition2.signal();
                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                    } finally {
                        lock.unlock();
                    }
                }
            });
            Thread t2 = new Thread(new Runnable() {
                @Override
                public void run() {
                    lock.lock();
                    try {
                        for (int i = 0; i < 10; i++) {
                            condition2.await();
                            System.out.println("b");
                            condition3.signal();
                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                    } finally {
                        lock.unlock();
                    }
                }
            });
            Thread t3 = new Thread(new Runnable() {
                @Override
                public void run() {
                    lock.lock();
                    try {
                        for (int i = 0; i < 10; i++) {
                            condition3.await();
                            System.out.println("c");
                            condition1.signal();
                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                    } finally {
                        lock.unlock();
                    }
                }
            });
            t1.start();
            t2.start();
            t3.start();
            try {
                Thread.sleep(1000);
                lock.lock();
                condition1.signal();
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                lock.unlock();
            }
        }
    
    

    方法5:用Semaphore实现

        public static void main(String[] args) {
            final Semaphore semaphore1 = new Semaphore(1);
            final Semaphore semaphore2 = new Semaphore(0);
            final Semaphore semaphore3 = new Semaphore(0);
            Thread t1 = new Thread(new Runnable() {
                @Override
                public void run() {
                    for (int i = 0; i < 10; i++) {
                        try {
                            semaphore1.acquire();
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                        System.out.println("a");
                        semaphore2.release();
                    }
                }
            });
            Thread t2 = new Thread(new Runnable() {
                @Override
                public void run() {
                    for (int i = 0; i < 10; i++) {
                        try {
                            semaphore2.acquire();
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                        System.out.println("b");
                        semaphore3.release();
                    }
                }
            });
            Thread t3 = new Thread(new Runnable() {
                @Override
                public void run() {
                    for (int i = 0; i < 10; i++) {
                        try {
                            semaphore3.acquire();
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                        System.out.println("c");
                        semaphore1.release();
                    }
                }
            });
            t1.start();
            t2.start();
            t3.start();
        }
    
  • 相关阅读:
    准备找工作了,求助于在成都的师兄、师姐们提供一些信息!大家都看看吧!
    大学课程名的中文翻译
    火箭今天赢了!!!
    如何成为一个受欢迎的人!
    理解DataSet的数据缓存机制
    餐桌上的不文雅行
    今天看到居然有100多个Gmail的邀请函,还没有Gmail的朋友留下邮箱地址!
    Update:代码行计算器(LinesCounter)
    找工作之“加班费”
    动画片里的Ajax
  • 原文地址:https://www.cnblogs.com/jyx140521/p/6747750.html
Copyright © 2020-2023  润新知