Java implements the main thread after the execution of multiple sub-threads is completed.

Java implements the main thread after the execution of multiple sub-threads is completed.

1. Business Scenario
1. When doing batch data processing, multiple stored procedures need to be executed. It takes about 10 minutes to execute a stored procedure. If you execute one by one, it will take a long time. After testing, it is found that the database resources are sufficient, and it has the ability to execute multiple stored procedures at the same time.
2. In java, when the stored procedure service is executed, the sub-thread execution is started asynchronously, and the stored procedure service is executed. The problem encountered is that both the main thread and the sub-thread are executing at the same time. At this time, the sub-thread result has not been executed yet, the main thread has been completed, and the result is fed back to the front end. (The stored procedure in the actual database is still being executed..)
3. The requirement is: The main thread needs to wait for the sub-thread to complete the execution before returning the result to the front end.
4. The approximate business code is as follows:
public class MultiThread {
	public static void main(String[] args) {
		System.out.println("Main thread starts executing...");
		for (int i = 0; i < 3; i++) {
			new Thread(){
				@Override
				public void run() {
					try {
						System.out.println(Thread.currentThread().getName()+"Start executing the stored procedure..");						
						Thread.sleep(2000);
						System.out.println(Thread.currentThread().getName()+"The stored procedure is executed...");						
					} catch (InterruptedException e) {
						e.printStackTrace ();
					}
				};
			}.start();
		}
		System.out.println("The main thread is finished....");
	}
}

5. The execution result is as follows:
The main thread starts executing....
The main thread has finished executing....
Thread-2 starts executing the stored procedure..
Thread-0 starts executing the stored procedure..
Thread-1 starts executing the stored procedure..
Thread-2 stored procedure execution is complete...
Thread-0 stored procedure execution is complete...
Thread-1 stored procedure execution is complete...

6. Obviously, the result does not meet the business requirements. The business requirements are:
The main thread starts executing....
Thread-0 starts executing the stored procedure..
...
Thread-2 stored procedure execution is complete...
The main thread has finished executing....

Second, the problem solution one (CountDownLatch) implementation
1. CountDownLatch: It is implemented by a counter, and the initial value of the counter is the number of threads. Every time a thread completes its task, the value of the counter is decremented by 1. When the counter value reaches 0, it indicates that all threads have completed the task, and then the thread waiting on the latch can resume executing the task. ( What is CountDownLatch )
2. The code is implemented as follows:
public class MultiThreadToCountDownLatch {
	public static void main(String[] args) throws InterruptedException {
		//1. Create a CountDownLatch object and set the number of child threads to be counted
		final CountDownLatch latch=new CountDownLatch(3);
		System.out.println("Main thread starts executing...");
		for (int i = 0; i < 3; i++) {
			new Thread(){
				@Override
				public void run() {
					try {
						System.out.println(Thread.currentThread().getName()+"  开始执行存储过程..");						
						Thread.sleep(2000);
						System.out.println(Thread.currentThread().getName()+"  存储过程执行完毕...");						
						//2、子线程执行完毕,计数减1
						latch.countDown();
					} catch (InterruptedException e) {
						e.printStackTrace();
					}
				};
			}.start();
		}
		System.out.println("等待子线程执行完毕...");
		//3、 当前线程挂起等待 
		latch.await();
		System.out.println("主线程执行完毕....");
	}
}

3、 执行结果如下:
主线程开始执行....
等待子线程执行完毕...
Thread-1 开始执行存储过程..
Thread-2 开始执行存储过程..
Thread-0 开始执行存储过程..
Thread-1 存储过程执行完毕...
Thread-2 存储过程执行完毕...
Thread-0 存储过程执行完毕...
主线程执行完毕....
4、 由执行结果可知,已经达到业务需求了。


三、其他实现方法补充
1、CyclicBarrier 和 Semaphore: http://www.importnew.com/21889.html
2、 使用 join()及 CountDownLatch 和 CyclicBarrier 区别: http://blog.csdn.net/u011277123/article/details/54015755




Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325860524&siteId=291194637