某天,在某个公众号上看到大佬的一份面经,里边出现了这道题.
原题描述是这样的:
使用两个线程打印数字1到100。要求:一个线程打印奇数,另一个打印偶数,并且按照1到100从小到大的顺序正确打印。
下面直接上代码,给出我的实现:
1. 使用Lock实现
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
   | package me.chan;
  import java.util.concurrent.locks.ReentrantLock;
  public class Sout1To100 {
      private static final ReentrantLock lock = new ReentrantLock();          private static volatile int num = 1;          private static volatile boolean flag = false;          public static void main(String[] args) throws InterruptedException {         Thread t1 = new Thread(()->{             while (num <= 100) {                 if (flag) {                     try {                         lock.lock();                         System.out.println(Thread.currentThread().getName()+":"+num);                         ++num;                         flag = false;                     } finally {                         lock.unlock();                     }                                      }             }         });                  Thread t2 = new Thread(()->{                          while (num <= 100) {                 if (!flag) {                     try {                         lock.lock();                         System.out.println(Thread.currentThread().getName()+":"+num);                         ++num;                         flag = true;                     } finally {                         lock.unlock();                     }                 }             }         });                  t1.start();         t2.start();         t1.join();         t2.join();     }
   | 
 
2. 使用notify()和wait()实现
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
   | package me.chan;
  public class Sout1To100 {
      private static final Object lock = new Object();          private static volatile int num = 1;          private static volatile boolean flag = true;           public static void main(String[] args) throws InterruptedException {         Thread t1 = new Thread(()->{                          while (num <= 100) {                 if (flag) {                                          synchronized(lock) {                         lock.notify();                         System.out.println(Thread.currentThread().getName()+":"+num);                         ++num;                         try {                             flag = false;                             lock.wait(10);                         } catch (Exception e) {                             e.printStackTrace();                         }                                              }                 }             }         });                  Thread t2 = new Thread(()->{             while (num <= 100) {                 if (!flag) {                     synchronized(lock) {                         lock.notify();                         System.out.println(Thread.currentThread().getName()+":"+num);                         ++num;                         try {                             flag = true;                             lock.wait(10);                         } catch (Exception e) {                             e.printStackTrace();                         }                     }                 }             }         });                  t1.start();         t2.start();         t1.join();         t2.join();              }
  }
   |