前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Java 指定线程执行顺序(三种方式)

Java 指定线程执行顺序(三种方式)

作者头像
九州暮云
发布2019-08-21 11:18:45
9240
发布2019-08-21 11:18:45
举报
文章被收录于专栏:九州牧云

方法一:通过共享对象锁加上可见变量来实现

代码语言:javascript
复制
/**
 * 指定线程执行顺序:通过synchronized共享对象锁加上volatile可见变量来实现
 */
public class ThreadOrder {

    private volatile int orderNum = 1;

    public synchronized void methodA() {
        try {
            while (orderNum != 1) {
                wait();
            }
            for (int i = 0; i < 2; i++) {
                System.out.println("AAAAA");
            }
            orderNum = 2;
            notifyAll();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    public synchronized void methodB() {
        try {
            while (orderNum != 2) {
                wait();
            }
            for (int i = 0; i < 2; i++) {
                System.out.println("BBBBB");
            }
            orderNum = 3;
            notifyAll();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    public synchronized void methodC() {
        try {
            while (orderNum != 3) {
                wait();
            }
            for (int i = 0; i < 2; i++) {
                System.out.println("CCCCC");
            }
            orderNum = 1;
            notifyAll();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}

测试类:

代码语言:javascript
复制
package com.example.concurrent;

import org.junit.Test;

public class ThreadOrderTest {

	/**
	 * 指定线程执行顺序:通过共享对象锁加上可见变量来实现
	 * 
	 * @throws Exception
	 */
	@Test
	public void test() throws Exception {
		ThreadOrder threadOrder = new ThreadOrder();

		Thread thread1 = new Thread(new Runnable() {

			@Override
			public void run() {
				// TODO Auto-generated method stub
				threadOrder.methodA();
			}
		});

		Thread thread2 = new Thread(new Runnable() {

			@Override
			public void run() {
				// TODO Auto-generated method stub
				threadOrder.methodB();
			}
		});

		Thread thread3 = new Thread(new Runnable() {

			@Override
			public void run() {
				// TODO Auto-generated method stub
				threadOrder.methodC();
			}
		});

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

执行结果:

代码语言:javascript
复制
AAA
AAA
BBB
BBB
CCC
CCC

可以看到线程的启动按顺序执行了。共享对象锁,可以保证每个方法只能同时有一个线程进入,配合wait和notifyAll方法,可以启动或者唤醒线程。

方法二:通过主线程Join()

join()方法的意思是等待线程执行完程序后死亡。

测试类:

代码语言:javascript
复制
package com.example.concurrent;

import org.junit.Test;

public class ThreadOrderTest {

	/**
	 * 通过主线程join()
	 * 
	 * @throws Exception
	 */
	@Test
	public void test2() throws Exception {
		Thread thread1 = new Thread(new Runnable() {

			@Override
			public void run() {
				// TODO Auto-generated method stub
				System.out.println("AAA");
			}
		});

		Thread thread2 = new Thread(new Runnable() {

			@Override
			public void run() {
				// TODO Auto-generated method stub
				System.out.println("BBB");
			}
		});

		Thread thread3 = new Thread(new Runnable() {

			@Override
			public void run() {
				// TODO Auto-generated method stub
				System.out.println("CCC");
			}
		});

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

执行结果:

代码语言:javascript
复制
AAA
BBB
CCC

方法三:通过线程执行时Join()

代码语言:javascript
复制
class T1 extends Thread {
    public void run(){
        Random random = new Random();
        try {
            Thread.sleep(random.nextInt(1000));
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("in T1");
    }
}

class T2 extends Thread{
    private Thread thread;
    public T2(Thread thread) {
        this.thread = thread;
    }

    public void run(){
        try {
            thread.join();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("in T2");
    }
}

class T3 extends Thread{
    private Thread thread;
    public T3(Thread thread) {
        this.thread = thread;
    }

    public void run(){
        try {
            thread.join();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("in T3");
    }
}

public class Test {
    public static void main(String[] args) throws InterruptedException {
        T1 t1 = new T1();
        T2 t2 = new T2(t1);
        T3 t3 = new T3(t2);
        t2.start();
        t1.start();
        t3.start();
    }
}

执行结果:

代码语言:javascript
复制
in T1
in T2
in T3
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 方法一:通过共享对象锁加上可见变量来实现
  • 方法二:通过主线程Join()
  • 方法三:通过线程执行时Join()
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档