Multithreading order to perform the method outlined

(1) using the join () method  

  In the process of executing a plurality of threads, if required by the need for the execution order between the threads, you can join the Thread class () method, to join easily explained if the thread is:


   The calling thread to wait before they can join the thread execution to continue downward after completion


  After example, we need to get the main thread to obtain an output value of a Thread1, this way you have to let Thread1 executed, the contents of the main thread before proceeding:

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 () method, and notify () method

   

thread.test Package Penalty for; 

/ ** 
 * 
 * the wait (): Object method, the role is to make the current thread into the wait state, at the same time, wait () also makes the current thread releases the lock it holds. "Until another thread invokes the object 
 * notify () method or the notifyAll () method", the current thread wakes up (into the "ready state") 
 * 
 * notify () and notifyAll (): is a method Object, the role is to wake up the current thread waiting on the object; notify () is to awaken a single thread, notifyAll () is a wake-up all the threads. 
 * 
 * The wait (Long timeout): Let the current thread in a "wait (blocking) state," "() method or the notifyAll () until another thread invokes the object of the Notify 
 * method, or a specified amount of time" and the current thread is wake-up (into the "ready state"). 
 * 
 * Scenarios: Java to achieve producer-consumer approach. 
 * / 
Public class WaitTest { 

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

	/ ** 
	 ? * Why add these two identification state if no state identification, already running when t1 over t2 just run, t2 wake up in waiting t1 t2 always lead to a wait state 
	 * /
	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();
	}
}

  

 

Guess you like

Origin www.cnblogs.com/dashenaichicha/p/12596024.html