java thread join method method Introduction

About this blog tell us join method java threads, join is to implement thread synchronization, multi-threaded parallel execution of the original method can be turned into a serial execution

As shown in FIG codes, are executed in parallel

public class ThreadTest {
    //private static final Long count = 10000L;
    public static void main(String[] args){
        long base = System.currentTimeMillis();
        try {
            ThreadJoin t1 = new ThreadJoin("线程1");
            ThreadJoin t2 = new ThreadJoin("线程2");
            //t1.join();
            t1.start();
            t1.join();
            t2.start();

        } catch (Exception e) {
            e.printStackTrace();
        }
        long time = System.currentTimeMillis() - base;
        System.out.println("执行时间:"+time);
    }
    
}
class ThreadJoin extends Thread{
    private static final Long count = 10L;

    public ThreadJoin(String name){
        super(name);
    }

    @Override
    public void run() {
        //super.run();
        for(int i = 1; i <= count; i ++){
            System.out.println(this.getName()+":"+i);
        }
    }
}

Print out the information, it is like this

执行时间:0
线程1:1
线程2:1
线程2:2
线程2:3
线程2:4
线程2:5
线程2:6
线程2:7
线程2:8
线程2:9
线程2:10
线程1:2
线程1:3
线程1:4
线程1:5
线程1:6
线程1:7
线程1:8
线程1:9
线程1:10

To achieve serial execution, you can add join method to achieve after the completion of the implementation of the thread 1 starts executing the thread 2, which is the serial execution

public class ThreadTest {
    //private static final Long count = 10000L;
    public static void main(String[] args){
        long base = System.currentTimeMillis();
        try {
            ThreadJoin t1 = new ThreadJoin("线程1");
            ThreadJoin t2 = new ThreadJoin("线程2");
            //t1.join();
            t1.start();
            t1.join();
            t2.start();

        } catch (Exception e) {
            e.printStackTrace();
        }
        long time = System.currentTimeMillis() - base;
        System.out.println("执行时间:"+time);
    }
    
}
class ThreadJoin extends Thread{
    private static final Long count = 10L;

    public ThreadJoin(String name){
        super(name);
    }

    @Override
    public void run() {
        //super.run();
        for(int i = 1; i <= count; i ++){
            System.out.println(this.getName()+":"+i);
        }
    }
}
线程1:1
线程1:2
线程1:3
线程1:4
线程1:5
线程1:6
线程1:7
线程1:8
线程1:9
线程1:10
执行时间:0
线程2:1
线程2:2
线程2:3
线程2:4
线程2:5
线程2:6
线程2:7
线程2:8
线程2:9
线程2:10

Looking at the results from the execution, it is already a serial execution threads

Therefore, the above example is transferred to the scene join method, that must be executed to complete the thread 1, before the implementation of the main thread main

The role of join method is, for example, when adjusting join Method B A thread in the thread, the thread execution is completed first B, then A will continue the thread

ok, the above method is to join without adjusting the parameters, can also add parameters, such as thread A.join(10);, thread A that is performed after 10s, the thread continues B

Note: join time parameter of default, default is 0, that is join () is equivalent to the Join (0);

/**
     * Waits for this thread to die.
     *
     * <p> An invocation of this method behaves in exactly the same
     * way as the invocation
     *
     * <blockquote>
     * {@linkplain #join(long) join}{@code (0)}
     * </blockquote>
     *
     * @throws  InterruptedException
     *          if any thread has interrupted the current thread. The
     *          <i>interrupted status</i> of the current thread is
     *          cleared when this exception is thrown.
     */
    public final void join() throws InterruptedException {
        join(0);
    }

Thread class in the source code, you can see the default values ​​0, 0 and then this is what does this mean? 0 is not that the implementation of 0s, but rather meant to be A thread execution is complete before continuing execution thread B

ok, then why the call to join the thread synchronization methods can be achieved? We simply look at the code:

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");
        }
        //执行时间为0或者缺省情况
        if (millis == 0) {
            while (isAlive()) {//表示线程还没执行好
                wait(0);//调用线程的wait方法
            }
        } else {//执行时间大于0的情况
            while (isAlive()) {
                long delay = millis - now;//循环计算延期时间
                if (delay <= 0) {
                    break;
                }
                wait(delay);//同样调用线程的wait方法
                now = System.currentTimeMillis() - base;
            }
        }
    }

ok, I looked at the source code, it is quite easy to understand, in fact, call the scene wait way to achieve thread synchronization

Guess you like

Origin www.cnblogs.com/mzq123/p/11582281.html
Recommended