Use 3 threads, 1 to print X, 1 to print Y, and 1 to print Z, simultaneously performing 10 consecutive prints of “XYZ”

Problem analysis: The core is that only one task is being executed at the same time, and the execution right is given to a specific thread after the current task is executed. We abstract executive power as a resource. The model can be abstracted to mean that threads must check if they have the resources they need to execute if they want to execute, and if they do, they can execute without the resources being acquired by other threads while they are holding them. First of all, we thought that the Lock is mutual, and we can realize it through this feature. Next, we respectively use synchronized+notify Lock+Condition and CAS operation to achieve it

Lock+Condition condition

public class Test1 { public static void main(String[] args) { Lock lock = new ReentrantLock(); Condition conditionX = lock.newCondition(); Condition conditionY = lock.newCondition(); Condition conditionZ = lock.newCondition(); int time = 10; Thread x = new Thread(new Task("X", time, lock, conditionX, conditionY)); Thread y = new Thread(new Task("Y", time, lock, conditionY, conditionZ)); Thread z = new Thread(new Task("Z", time, lock, conditionZ, conditionX)); x.start(); y.start(); z.start(); } static class Task implements Runnable {/** * private String name; /** * private int time; /** * private Lock Lock; /** * private Condition wait; /** * private Condition notify; public Task(String name, int time, Lock lock, Condition wait, Condition notify) { this.name = name; this.time = time; this.lock = lock; this.wait = wait; this.notify = notify; } @Override public void run() { int count = 0; while (count < time) { count++; lock.lock(); System.out.println(name); try { notify.signal(); wait.await(); } catch (InterruptedException ignore) { } } } }Copy the code

}

Synchronized + notify way

public class Sync { private static int next = 0; public static void main(String[] args) { Object lock = new Object(); new Thread(new Task(0, lock, "X")).start(); new Thread(new Task(1, lock, "Y")).start(); new Thread(new Task(2, lock, "Z")).start(); } static class Task implements Runnable { private int t; private Object lock; private String name; public Task(int t, Object lock, String name) { this.t = t; this.lock = lock; this.name = name; } @Override public void run() { while (next < 30) { synchronized (lock) { if (next % 3 == t) { System.out.println(name);  next++; } try { lock.notifyAll(); lock.wait(); } catch (Exception ignore) { } } } } }Copy the code

}

CAS way

public class Test1NoLock { private static final int run=3; public static void main(String[] args) throws InterruptedException { AtomicInteger integer=new AtomicInteger(0); New Thread (new Task (" X ", 10, 1, integer)). The start (); New Thread (new Task (" Y ", 10,1,2, integer)). The start (); New Thread (new Task (" Z ", 0, 10 integer)). The start (); } static class Task implements Runnable{ private String name; private int time; private int expect; private int update; private AtomicInteger integer; public Task(String name, int time,int expect, int update, AtomicInteger integer) { this.name = name; this.time=time; this.expect = expect; this.update = update; this.integer = integer; } @Override public void run() { int count=0; while (count<time) { if (integer.compareAndSet(expect,run)){ count++; System.out.println(name); integer.compareAndSet(3,update); }}}}Copy the code

}