Java multithreading learning

1. Join: Make the calling thread wait for the specified thread to finish executing before continuing to execute

package zsw.study.join;

import java.util.concurrent.locks.ReentrantLock;

/**
 * Created by Administrator on 2017-01-16.
 * join method demonstration
 */
public class JoinDemo extends Thread{

    public static int count = 0;

    private ReentrantLock lock = null;

    @Override
    public void run() {
        for (int i = 0;i<10;i++){
            lock.lock();
           try{
               count++;
           }finally {
               lock.unlock();
           }
        }
    }

    public JoinDemo(ReentrantLock lock) {
        this.lock = lock;
    }

    public static void main(String[] args) throws InterruptedException {
        ReentrantLock reentrantLock = new ReentrantLock();
        Thread t1 = new JoinDemo(reentrantLock);
        Thread t2 = new JoinDemo(reentrantLock);
        t1.start();t2.start();
        //Call t1 t2 here, so that the thread executing main waits for the execution of t1 and t2 to complete the execution of the last statement
        t1.join();t2.join();
        System.err.println(JoinDemo.count);
    }
}

 

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326928474&siteId=291194637