Java并发面试题:三个线程轮流打印十次abc
2017-04-22 14:24
447 查看
方法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(); }
相关文章推荐
- Java并发编程--三个线程顺序打印ABC
- java多线程面试题:三个线程顺序打印ABC,重复10次
- JAVA三个线程依次打印ABC
- 三个线程轮流执行顺序打印ABC(三):使用Lock实现
- 有三个线程ID分别是ABC,用多线程编程在屏幕上循环打印ABCABC....十次
- 循环打印三个线程,ABC 十次
- java并发---三线程打印ABC的问题
- 使用Java 多线程编程 让三个线程轮流输出ABC,循环10次后结束
- 三个线程轮流执行顺序打印ABC(二):使用Lock+Condition实现
- java实现【有三个线程ID分别是A、B、C,请有多线编程实现,在屏幕上循环打印10次ABC.】
- 面试题--三个线程循环打印ABC10次的几种解决方法
- java 线程上机题 循环唤醒 三个线程轮流打印 顺序 A线程->B线程->C线程
- 【面试】--三个线程轮流打印ABC
- 面试题--三个线程循环打印ABC 10次:另类解决方法
- 面试题--三个线程循环打印ABC10次的几种解决方法
- 迅雷笔试题 (JAVA多线程)启动三个线程,分别打印A B C,现在写一个程序 循环打印ABCABCABC
- 写一个程序三个线程分别输出A,B,C, 顺序输出ABC十次,用java实现
- 三个线程轮流执行顺序打印ABC(一):使用Semaphore实现
- 关于线程的面试题:利用三个线程,循环打印ABC三个数十遍
- 一道Java线程面试题。3个线程顺序打印10次ABC.