zoukankan      html  css  js  c++  java
  • 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();
        }
    
  • 相关阅读:
    vmware Unable to open kernel device "\.Globalvmx86": The system cannot find the file 的解决方法
    nc和telnet配合使用
    linux下批量替换文件内容
    Linux动态库的导出控制
    goang Receiver & interface
    Go与C语言的互操作 cgo
    Go fsm
    Git多账号登陆
    mysql 安装与配置、使用
    Reverse Integer
  • 原文地址:https://www.cnblogs.com/jyx140521/p/6747750.html
Copyright © 2011-2022 走看看