题目:多线程交替打印1A2B3C……26Z
- 定义统一变量
1 2 3 4 5 6
| private static ExecutorService executorService = Executors.newSingleThreadExecutor(); private static ReentrantLock reentrantLock = new ReentrantLock(); private static volatile AtomicInteger ReadyToRun = new AtomicInteger(0); private static volatile int data = 1; private static Object object = new Object(); private static volatile boolean flag = true;
|
- 方法一
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
| public static void printABC3() { new Thread(new Runnable() { @Override public void run() { while (true) { if (data > 26) { ReadyToRun.set(1); System.out.println(Thread.currentThread().getName() + ": stop"); break; } while (ReadyToRun.get() == 0) { if (data > 26){ ReadyToRun.set(1); break; } System.out.println(Thread.currentThread().getName() + ": "+ data); ReadyToRun.set(1); }
} } },"t1").start();
new Thread(new Runnable() { @Override public void run() { while (true) { if (data >= 27){ break; } while (ReadyToRun.get() == 1) { System.out.println(Thread.currentThread().getName() + " : " + (char) (data + 64)); data++; if (data > 26) { System.out.println(Thread.currentThread().getName() + ": stop"); break; } ReadyToRun.set(0); } } } },"t2").start();
}
|
3.方法二:
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
|
public static void printABC2() { new Thread(new Runnable() { @Override public void run() { while (true) { synchronized (object) { if (data > 26) { System.out.println(Thread.currentThread().getName() + " stop" ); object.notify(); break; }
if (flag) { System.out.print(data); flag = false; try { object.notify(); object.wait(); } catch (InterruptedException e) { e.printStackTrace(); } } } } } },"t1").start();
new Thread(new Runnable() { @Override public void run() { while (true) { if (data > 26) { System.out.println(Thread.currentThread().getName() + " stop" ); break; } synchronized (object) { if (!flag) { System.out.print((char) (data + 64)); data++; flag = true; try { object.notify(); object.wait(); } catch (InterruptedException e) { e.printStackTrace(); } } } } } },"t2").start(); }
|
3:
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
| public static void printABC1() { new Thread(new Runnable() { @Override public void run() { while (true) { reentrantLock.lock(); if (data > 26) { reentrantLock.unlock(); break; } if (flag) { System.out.print(data); flag = false; } reentrantLock.unlock(); } } }, "thread-num").start();
new Thread(new Runnable() { @Override public void run() { while (true) { reentrantLock.lock(); if (data > 26) { reentrantLock.unlock(); break; } if (!flag) { System.out.print((char) (data + 64)); flag = true; data++; } reentrantLock.unlock(); } } }, "thread-char").start(); }
|
- newSingleThreadExecutor方法是保证提交的任务顺序执行
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| public static void printABC() { int[] data = new int[26]; for (int i = 0; i < 26; i++) { data[i] = i + 1; }
for (int i = 0; i < 26; i++) { int temp = data[i]; executorService.submit(new Runnable() { @Override public void run() { System.out.print(temp + "" + (char) (temp + 64)); } }); } }
|
本文地址:
http://byejd.github.io/2019/11/20/%E4%B8%A4%E4%B8%AA%E7%BA%BF%E7%A8%8B%E4%BA%A4%E6%9B%BF%E6%89%93%E5%8D%B0%E5%AD%97%E7%AC%A6/