Write code to use 3 threads, 1 thread to print X, 1 thread to print Y, and 1 thread to print Z, while performing 10 consecutive prints of "XYZ"Copy the code

We need two variables:

  • COUNT: 0,1,2,3,4,5,6,7,8,9
  • The other records that the thread prints flags: 0- prints X,1- prints Y,2- prints Z

1. The code is as follows:

package com.yuhl.right.ali;


import java.util.concurrent.TimeUnit;

/** */ /1, write code, use 3 threads, 1 thread to print X, 1 thread to print Y, 1 thread to print Z, at the same time execute 10 consecutive print "XYZ" */
public class PrintXYZ10Times2 {

    // Print times
    //private static volatile Integer COUNT = 0;
    private static Integer COUNT = 0;

    /** * Print label * 0: print label x * 1: print label Y * 2: print label Z */
    //private static volatile Integer FLAG = 0;
    private static Integer FLAG = 0;


    public static void main(String[] args) {


        // print logic for X
        new Thread(() -> {
            while (true) {// Always execute, variable changes must be written inside if
                if (FLAG == 0) {
                    System.out.print("The first"+(COUNT +1) +"Times print: X");
                    FLAG = 1;
                }
                try {
                    TimeUnit.MICROSECONDS.sleep(500);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }

            }
        }).start();

        //Y print logic
        new Thread(() -> {
            while (true) {
                if (FLAG == 1) {
                    System.out.print("Y ");
                    FLAG = 2;
                }
                try {
                    TimeUnit.MICROSECONDS.sleep(500);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }

            }
        }).start();
        
        //Z print logic
        new Thread(() -> {
            while (true) {
                if (FLAG == 2) {
                    System.out.println("Z ");
                    COUNT++;
                    if(COUNT == 10){
                        System.exit(0);
                    }
                    FLAG = 0;
                }
                try {
                    TimeUnit.MICROSECONDS.sleep(500);
                } catch(InterruptedException e) { e.printStackTrace(); } } }).start(); }}Copy the code

2. The running result is as follows:

"C: \ Program Files \ Java \ jdk1.8.0 _201 \ bin \ Java exe"1Times print: X Y Z th2Times print: X Y Z th3Times print: X Y Z th4Times print: X Y Z th5Times print: X Y Z th6Times print: X Y Z th7Times print: X Y Z th8Times print: X Y Z th9Times print: X Y Z th10X Y Z Process finished with exit code0
Copy the code

3. Write code in a way that’s easier to understand

3.1 PrintXYZ. Java

package com.yuhl.test001;

import java.util.concurrent.TimeUnit;

/ * * *@author yuhl
 * @Date 2020/10/29 21:46
 * @Classname PrintXYZ
 * @Description TODO
 */
public class PrintXYZ {
    // Print the number of times 1--10
    static volatile int count = 0;

    /** * 1: thread X print * 2: thread Y print * 3: thread Z print */
    static volatile int flag = 1;

    public static void main(String[] args) {

        new Thread(new Runnable(){

            @Override
            public void run(a) {
                while (true) {
                    if(flag == 1){
                        System.out.print("X ");/ / print X
                        try {
                            TimeUnit.SECONDS.sleep(1);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                        flag = 2; }}}},"X").start();

        new Thread(new Runnable(){

            @Override
            public void run(a) {
                while (true) {
                    if(flag == 2){
                        System.out.print("Y ");/ / print Y
                        try {
                            TimeUnit.SECONDS.sleep(1);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                        flag = 3; }}}},"Y").start();

        new Thread(new Runnable(){

            @Override
            public void run(a) {
                while (true) {
                    if(flag == 3){
                        System.out.print("Z ");/ / print Z
                        try {
                            TimeUnit.SECONDS.sleep(1);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }

                        if (count++ == 9) {// If the number of times is 10, the system stops
                            System.exit(0);// The system stops
                        }
                        System.out.println();/ / a newline
                        flag = 1; }}}},"Z").start(); }}Copy the code

3.2 Running Results

X Y Z 
X Y Z 
X Y Z 
X Y Z 
X Y Z 
X Y Z 
X Y Z 
X Y Z 
X Y Z 
X Y Z 
Process finished with exit code 0
Copy the code