java 多线程顺序输出ABC

这里写自定义目录标题

package com.thread;

import java.util.concurrent.Executor;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

public class SxAbc{

    static volatile int anInt=1;
    static Thread thread1=new Thread(){
        @Override
        public void run() {
           synchronized (this) {
               if (anInt != 1) {
                   try {
                       System.out.println("thread1线程开始阻塞");
                       this.wait();
                   } catch (InterruptedException e) {
                       e.printStackTrace();
                   }

               }
               try {
                   Thread.sleep(3000);
               } catch (InterruptedException e) {
                   e.printStackTrace();
               }
               System.out.println("A");
               anInt++;

           }
            synchronized(thread2) {
                thread2.notify();
            }

        }
    };

    static Thread thread2=new Thread(){
        @Override
        public void run() {
            synchronized (this) {
                if (anInt != 2) {
                    try {
                        System.out.println("thread2线程开始阻塞");
                        this.wait();
                        System.out.println("thread2线程被唤醒");
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
                try {
                    Thread.sleep(2000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println("B");
                anInt++;
            }
            synchronized(thread3) {
                thread3.notify();
            }
        }
    };
    static Thread thread3=new Thread(){
        @Override
        public void run() {
            synchronized (this) {
                if (anInt != 3) {
                    try {
                        System.out.println("thread3线程开始阻塞");
                        this.wait();
                        System.out.println("thread3线程被唤醒");
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println("C");

            }
        }
    };
    /**
     * 使用 wait 和 notify实现
     */
    public static void dxc(){
        //同时启动线程
        SxAbc sxAbc=new SxAbc();
        sxAbc.thread1.start();
        sxAbc.thread2.start();
        sxAbc.thread3.start();
    }


    /**
     * 线程池实现
     */
    public static void xcc(){
       // ExecutorService executorService= Executors.newSingleThreadExecutor();
        ExecutorService executorService= Executors.newFixedThreadPool(3);
        executorService.execute(new Thread(){
            @Override
            public void run() {
                System.out.println("A");
            }
        });
        executorService.execute(new Thread(){
            @Override
            public void run() {
                System.out.println("B");
            }
        });
        executorService.execute(new Thread(){
            @Override
            public void run() {
                System.out.println("C");
            }
        });
        executorService.shutdown();
    }

    /**
     * 使用join
     */
    public static void dxcjoin(){
        Thread thread1=new Thread(){
            @Override
            public void run() {
                try {
                    Thread.sleep(3000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println("thread1:"+Thread.currentThread().getId());
            }
        };
        Thread thread2=new Thread(){
            @Override
            public void run() {
                try {
                    thread1.join();
                    Thread.sleep(2000);
                    System.out.println("thread2:"+Thread.currentThread().getId());
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        };
        Thread thread3=new Thread(){
            @Override
            public void run() {
                try {
                    thread2.join();
                    Thread.sleep(1000);
                    System.out.println("thread3:"+Thread.currentThread().getId());
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        };
        thread1.start();
        thread2.start();
        thread3.start();
    }

    /**
     * 使用锁 保证线程顺序支持
     */
    public static void lockThread(){
        Runs runs=new Runs();
        Thread thread1=new Thread(new RunA(runs));
        Thread thread2=new Thread(new RunB(runs));
        Thread thread3=new Thread(new RunC(runs));
        thread1.start();
        thread2.start();
        thread3.start();
    }

    public static void main(String[] args) {
        /**
         * 使用 wait 和 notify实现
         */
       // SxAbc.dxc();
        /**
         * 线程池实现
         */
       // SxAbc.xcc();
        /**
         * 使用join
         */
       //SxAbc.dxcjoin();
        /**
         * 使用锁 保证线程顺序支持
         */
        lockThread();
    }



}
class Runs{
    private int status = 1;
    private ReentrantLock lock = new ReentrantLock();
    private Condition condition1 = lock.newCondition();
    private Condition condition2 = lock.newCondition();
    private Condition condition3 = lock.newCondition();
   public void printA(){
       lock.lock();
       try {
           if (status != 1) {
               condition1.await();
           }
           //Thread.sleep(3000);
           System.out.print("A");
           status=2;
           condition2.signal();
       }catch (InterruptedException  e){
           e.printStackTrace();
       }finally {
           lock.unlock();
       }
   }

   public void printB(){
       lock.lock();
       try {
           if(status!=2){
               condition2.await();
           }
           System.out.print("B");
           status=3;
           condition3.signal();

       }catch (InterruptedException  e){
           e.printStackTrace();
       }finally {
           lock.unlock();
       }
   }
    public void printC(){
        lock.lock();
        try {
            if(status!=3){
                condition3.await();
            }
            System.out.println("C");
            status=1;
            condition1.signal();
        }catch (InterruptedException  e){
            e.printStackTrace();
        }finally {
            lock.unlock();
        }
    }
}
class RunA implements Runnable{
    private Runs runs;
    public RunA(Runs runs){
        super();
        this.runs=runs;
    }
    @Override
    public void run() {
        for (int i = 0; i < 5; i++) {
            runs.printA();
        }
    }
}
class RunB implements Runnable{
    private Runs runs;
    public RunB(Runs runs){
        super();
        this.runs=runs;
    }
    @Override
    public void run() {
        for (int i = 0; i < 5; i++) {
            runs.printB();
        }
    }
}
class RunC implements Runnable{
    private Runs runs;
    public RunC(Runs runs){
        super();
        this.runs=runs;
    }
    @Override
    public void run() {
        for (int i = 0; i < 5; i++) {
            runs.printC();
        }
    }
}






评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值