java多线程实现顺序执行

1.使用线程的join方法

 public static void main(String[] args) {
        final Thread t1 = new Thread(new Runnable() {
            @Override
            public void run() {
                System.out.println("起床");
            }
        });
        final Thread t2 = new Thread(new Runnable() {
            @SneakyThrows
            @Override
            public void run() {
                t1.join();
                System.out.println("洗脸");
            }
        });
        final Thread t3 = new Thread(new Runnable() {
            @SneakyThrows
            @Override
            public void run() {
                t2.join();
                System.out.println("吃饭");
            }
        });
        t3.start();
        t2.start();
        t1.start();
    }

2.使用主线程的join方法

public static void main(String[] args) throws InterruptedException {
        final Thread t1 = new Thread(new Runnable() {
            @Override
            public void run() {
                System.out.println("起床");
            }
        });
        final Thread t2 = new Thread(new Runnable() {
            @Override
            public void run() {
                System.out.println("洗脸");
            }
        });
        final Thread t3 = new Thread(new Runnable() {
            @Override
            public void run() {
                System.out.println("吃饭");
            }
        });
        t1.start();
        t1.join();
        t2.start();
        t2.join();
        t3.start();
    }

3.使用线程的wait方法(实现生产者消费者)

public class ThreadA extends Thread {
    private MyServer server;

    public ThreadA(MyServer server) {
        this.server = server;
    }

    @Override
    public void run() {
        for (int i = 0; i < 5; i++) {
            server.print1();
        }
    }
}

public class ThreadB extends Thread {
    private MyServer server;

    public ThreadB(MyServer server) {
        this.server = server;
    }

    @Override
    public void run() {
        for (int i = 0; i < 5; i++) {
            server.print2();
        }
    }
}

public class ThreadC extends Thread {
    private MyServer server;

    public ThreadC(MyServer server) {
        this.server = server;
    }

    @Override
    public void run() {
        for (int i = 0; i < 5; i++) {
            server.print3();
        }
    }
}
public class MyServer {
    private volatile int flag = 1;

    public synchronized void print1() {
        while (flag != 1) {
            try {
                this.wait();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        System.out.println("线程1");
        flag = 2;
        this.notifyAll();
    }

    public synchronized void print2() {
        while (flag != 2) {
            try {
                this.wait();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        System.out.println("线程2");
        flag = 3;
        this.notifyAll();
    }

    public synchronized void print3() {
        while (flag != 3) {
            try {
                this.wait();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        System.out.println("线程3");
        flag = 1;
        this.notifyAll();
    }
}
public class Main1 {
    public static void main(String[] args) throws InterruptedException {
        MyServer server=new MyServer();
        ThreadA a=new ThreadA(server);
        ThreadB b=new ThreadB(server);
        ThreadC c=new ThreadC(server);
        b.start();
        c.start(); a.start();
    }
}

简单的就是这三种,还有其他的方式后续再添加

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值