Use of multithreaded join method

In many cases, the main thread creates and starts the child thread, and if a large amount of time-consuming operations are performed in the child thread, the main thread will usually end before the child thread ends.In this case, the join () method is used if the main thread wants to wait for the execution of the child thread to finish, such as when the child thread processes a data and the value in the data the main thread wants to fetch.The join() method waits for the thread object to be destroyed.Both join () and join(long) methods have the feature of releasing locks.

1.join() method

Thread Code

public class MyThread extends Thread{
	@Override
	public void run() {
		super.run();
		int sum = 0;
		for(int i = 0;i<100;i++){
			sum = sum + i +1;
		}
		System.out.println(sum + "    " +System.currentTimeMillis());
		System.currentTimeMillis();
		try {
			Thread.sleep(5000);
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}

Test code:

public class Run {

	public static void main(String[] args) {
		try {
			MyThread mythread = new MyThread();
			mythread.start();
			mythread.join();
			System.out.println("main    " + System.currentTimeMillis());
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}

Run result:
················

2.join(long) method

The parameter in the method join(long) is to set the wait time and release the lock when the time exceeds the long time.

3. Verification that the join (long) method can release locks

By comparing with the sleep(long) method, the release lock characteristic of the join() method can be reflected.Because the sleep(long) method does not release the lock on the current object.

Code for ThreadB thread:

public class ThreadB extends Thread{
	@Override
	public void run() {
		super.run();
		System.out.println("Get into run Method    " + System.currentTimeMillis());
		try {
			Thread.sleep(5000);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
	}
	
	synchronized public void printString(){
		System.out.println("Enter printString Method    " + System.currentTimeMillis());
	}
}

ThreadA thread: (Run the run method in ThreadB)

public class ThreadA extends Thread{
	private ThreadB threadb;
	public ThreadA(ThreadB threadb){
		this.threadb = threadb;
	}
	@Override
	public void run() {
		super.run();
		synchronized(threadb){
			threadb.start();
			try {
				// There are differences
//				threadb.join(6000);
				Thread.sleep(6000);
				System.out.println("ThreadA in" + System.currentTimeMillis());
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
	}
}

ThreadC thread: (Run the synchronization method in ThreadB)

public class ThreadC extends Thread{
	private ThreadB threadb;
	public ThreadC(ThreadB threadb){
		this.threadb = threadb;
	}
	@Override
	public void run() {
		super.run();
		threadb.printString();
	}
}

Run Code:

public class Run {
	public static void main(String[] args){
		ThreadB b = new ThreadB();
		ThreadA a = new ThreadA(b);
		ThreadC c = new ThreadC(b);
		a.start();
		c.start();
	}
}

1) First we'll turn on threadb.join(6000); comment, turn on Thread.sleep(6000).Run result:

You can see that the locking methods in ThreadB are running synchronously.
2) Turn threadb.sleep(6000); comment, turn Thread.join(6000).Run result:

Locking methods in ThreadB run asynchronously.
By comparison, it can be concluded that the join(long) method releases object locks.

                                                                                                                                                                                                                                                                                                                                                

Added by RSprinkel on Thu, 08 Aug 2019 06:44:37 +0300