Thread join in the role and principles

This paper introduces the content
  1. Thread.join 作用
  2. The principle of Thread.join
  3. When will use Thread.join
  4. Spread

1 role

When the circumstances of each single-threaded multi-threaded cpu will obtain the right to perform in the competition, the order is served to wrap, first to ensure the child thread execution order, controlled by the main thread Thread.join let the child thread executing the re-implementation of the next child thread to achieve the bottom of the principles of the use of Object in wait

dome1


public class ThreadJoin {

    static Thread thread1=new Thread(new Runnable() {
        @Override
        public void run() {
            System.out.println("thread1");
        }
    });
    static Thread thread2=new Thread(new Runnable() {
        @Override
        public void run() {
            System.out.println("thread2");
        }
    });
    static Thread thread3=new Thread(new Runnable() {
        @Override
        public void run() {
            System.out.println("thread3");
        }
    });

    public static void main(String[] args) throws InterruptedException {
        thread1.run();
        thread1.join();
        thread2.run();
        thread1.join();
        thread3.run();
        thread3.join();
    }

}

Results of the:

thread1
thread2
thread3

Process finished with exit code 0

Show:
When the code without thread.join (); in a multithreaded execution in order to ensure that they are not, the output is disorderly, after adding there is a need to perform.

principle

See the picture understanding of the principles
Here Insert Picture Description
View source code:

....
public final void join() throws InterruptedException {
        join(0);
}
    ....
 public final synchronized void join(long millis)
    throws InterruptedException {
        long base = System.currentTimeMillis();
        long now = 0;

        if (millis < 0) {
            throw new IllegalArgumentException("timeout value is negative");
        }

        if (millis == 0) {
            while (isAlive()) {
                wait(0);
            }
        } else {
            while (isAlive()) {
                long delay = millis - now;
                if (delay <= 0) {
                    break;
                }
                wait(delay);
                now = System.currentTimeMillis() - base;
            }
        }
    }

Summary: When the main thread calls run sub-thread, the child thread calls join, will be the main thread blocking can be seen through the join source synchronized, wait () method waits for executing the re-execution of the main thread to perform the second sub-thread, to ensure that each thread and then executing the next sub-thread execution

When is the use of

When multiple threads need, such as columns: t1, t2 thread, the results need t1 t1 when performing other business logic can be employed Thread.join child thread wait finished performing logic operations do the following
with particular reference to deom2

public void joinDemo(){
   //....
   Thread t=new Thread(payService);
   t.start();
   //.... 
   //其他业务逻辑处理,不需要确定t线程是否执行完
   insertData();
   //后续的处理,需要依赖t线程的执行结果,可以在这里调用join方法等待t线程执行结束
   t.join();
}

Spread

We can also be reached by way of a multi-threaded team FIFO order execution
ideas, through the thread pool Executors.newSingleThreadExecutor () single-threaded mode, use the bottom of FIFO (First In First Out) multi-threaded execution order

Reference dome1 modification

 ExecutorService executorService=Executors.newSingleThreadExecutor();
        executorService.execute( thread1);
        executorService.execute( thread2);
        executorService.execute( thread3);
        executorService.isShutdown();

The output agreement

There are understood not appropriate places to welcome Tucao ~ ~

Published 10 original articles · won praise 9 · views 447

Guess you like

Origin blog.csdn.net/weixin_43829047/article/details/104903154