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();
        }
    
  • 相关阅读:
    Flash Builder 使用
    解决谷歌地图偏移问题
    南京垃圾处理场分布图-益云地图
    在Oracle Spatial中增加Web Mercator投影坐标系
    学习和使用 Styled Layer Descriptor SLD样式文件
    jmeter安装教程
    Linux常见命令更新中...
    Python并发编程(线程队列,协程,Greenlet,Gevent)
    Python并发编程(线程,Threading模块,守护线程,gil锁,)
    Python并发编程(管道,数据共享,信号量,进程池)
  • 原文地址:https://www.cnblogs.com/jyx140521/p/6747750.html
Copyright © 2011-2022 走看看