线程-----多线程循环打印ABC10次

问题:开启3个线程,这3个线程的ID分别为A.B.C,每个线程将自己的ID依次在屏幕上打印10次,要求输出结果必须按照ABC的顺序显示

  • 方法一:
//ThreadName类

public class ThreadName0511 {
    private Integer index;//当前执行线程ID
    
    public Integer getIndex() {
        return index;
    }

    public void setIndex(Integer index) {
        this.index = index;
    }
} 

//ThreadDemo类
public class ThreadDemo0511 extends Thread {
        private ThreadName0511 threadName;//线程间传递对象
        private Integer number;//当前线程编号

    //构造函数
        public ThreadDemo0511(ThreadName0511 threadName,Integer number) {
            this.threadName = threadName;
            this.number = number;
        }

        @Override
        public void run() {
            int i = 1;
            while(i <= 10) {
                synchronized (threadName){
                    //如果当前的线程编号不是所需要的线程编号,就调用wait()方法
                    while (number != threadName.getIndex()){
                        try{
                            threadName.wait();
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                    //否则就打印
                    System.out.print(Thread.currentThread().getName());
                    //下一个线程的编号
                    threadName.setIndex((number+1)%3);
                    threadName.notifyAll();
                    i++;
                }
            }
        }
    }

//主函数
public class ThreadTest511 {
    public static void main(String[] args) {
        ThreadName0511 threadName = new ThreadName0511();
        threadName.setIndex(0);
        Thread T0 = new Thread(new ThreadDemo0511(threadName,0));
        Thread T1 = new Thread(new ThreadDemo0511(threadName,1));
        Thread T2 = new Thread(new ThreadDemo0511(threadName,2));
        T0.setName("A");
        T1.setName("B");
        T2.setName("C");
        T0.start();
        T1.start();
        T2.start();
    }
}

打印结果:
在这里插入图片描述

  • 方法二:
public class TestABC {
    private static int COUNT  = 0;

    public static void main(String[] args) {
        Lock lock = new Lock();

        Thread thread1 = new Thread(new Runnable(){
            @Override
            public void run() {
                while(COUNT <= 30) {
                    try {
                        lock.lock();
                        if(COUNT %3==0) {
                            System.out.print("A");
                            COUNT ++;
                        }
                        lock.unlock();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
            });

        Thread thread2= new Thread(new Runnable(){
            @Override
            public void run() {
                while(COUNT <= 30) {
                    try {
                        lock.lock();
                        if(COUNT%3==1) {
                            System.out.print("B");
                            COUNT++;
                        }
                        lock.unlock();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        });

        Thread thread3 = new Thread(new Runnable(){
            @Override
            public void run() {
                while(COUNT <= 30) {
                    try {
                        lock.lock();
                        if(COUNT%3==2) {
                            System.out.print("C");
                            COUNT++;
                        }
                        lock.unlock();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        });

        thread1.start();
        thread2.start();
        thread3.start();
        }
}

你可能感兴趣的:(总结)