JS Bin

多线程顺序执行方法概述

(1)使用join()方法  

  在多个线程的执行过程中,若需要对于线程之间的执行顺序有要求的话,可以使用Thread类的join()方法,简单地对于join线程进行解释的话就是:


   调用线程要等待join线程执行完成之后才可以继续向下执行


  比如我们需要让主线程得到一个Thread1的输出值,这样子就得让Thread1执行完成之后,主线程的内容再继续执行:

package thread.test;

public class JoinFunctionTest {

	public static void main(String[] args) throws InterruptedException {
		// 测试join()
		Thread thread1 = new Thread(new Runnable() {

			@Override
			public void run() {
				// TODO Auto-generated method stub
				System.out.println("Thread1执行");
			}
		});
                thread1.start();
		thread1.join();
		System.out.println("等thread1执行完再执行主线程");
	}
}        

  

 

   

package thread.test;

public class JoinFunctionTest {

	public static void main(String[] args) throws InterruptedException {
		// 测试join()
		final Thread thread1 = new Thread(new Runnable() {

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

		final Thread thread2 = new Thread(new Runnable() {
			@Override
			public void run() {
				try {
					thread1.join();
					System.out.println("Thread2执行");
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
			}
		});

		Thread thread3 = new Thread(new Runnable() {
			@Override
			public void run() {
				try {
					thread2.join();
					System.out.println("Thread3执行");
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
			}
		});

		System.out.println("开始:");
		System.out.println("Thread1上线...");
		thread3.start();
		System.out.println("Thread2上线...");
		thread1.start();
		System.out.println("Thread3上线...");
		thread2.start();
	}
}

  

(2)使用wait()方法和notify()方法

   

package thread.test;

/**
 *
 * wait():是Object的方法,作用是让当前线程进入等待状态,同时,wait()也会让当前线程释放它所持有的锁。“直到其他线程调用此对象的
 * notify() 方法或 notifyAll() 方法”,当前线程被唤醒(进入“就绪状态”)
 * 
 * notify()和notifyAll():是Object的方法,作用则是唤醒当前对象上的等待线程;notify()是唤醒单个线程,而notifyAll()是唤醒所有的线程。
 * 
 * wait(long timeout):让当前线程处于“等待(阻塞)状态”,“直到其他线程调用此对象的notify()方法或 notifyAll()
 * 方法,或者超过指定的时间量”,当前线程被唤醒(进入“就绪状态”)。
 * 
 * 应用场景:Java实现生产者消费者的方式。
 */
public class WaitTest {

	private static Object myLock1 = new Object();
	private static Object myLock2 = new Object();

	/**
	 * 为什么要加这两个标识状态? 如果没有状态标识,当t1已经运行完了t2才运行,t2在等待t1唤醒导致t2永远处于等待状态
	 */
	private static Boolean t1Run = false;
	private static Boolean t2Run = false;

	public static void main(String[] args) {

		final Thread thread1 = new Thread(new Runnable() {
			@Override
			public void run() {
				synchronized (myLock1) {
					System.out.println("Thread1开始执行...");
					t1Run = true;
					myLock1.notify();
				}
			}
		});

		final Thread thread2 = new Thread(new Runnable() {
			@Override
			public void run() {
				synchronized (myLock1) {
					try {
						if (!t1Run) {
							System.out.println("Thread2休息会...");
							myLock1.wait();
						}
						synchronized (myLock2) {
							System.out.println("Thread2开始执行..");
							myLock2.notify();
						}
					} catch (InterruptedException e) {
						e.printStackTrace();
					}
				}
			}
		});

		Thread thread3 = new Thread(new Runnable() {
			@Override
			public void run() {
				synchronized (myLock2) {
					try {
						if (!t2Run) {
							System.out.println("Thread3先休息会...");
							myLock2.wait();
						}
						System.out.println("Thread3开始执行..");
					} catch (InterruptedException e) {
						e.printStackTrace();
					}
				}
			}
		});

		System.out.println("开始:");
		System.out.println("Thread1来了...");
		thread3.start();
		System.out.println("Thread2来了...");
		thread1.start();
		System.out.println("Thread3来了...");
		thread2.start();
	}
}

  

 

posted @ 2020-03-29 23:52  左五六  阅读(982)  评论(0编辑  收藏  举报