Thread join method Detailed

Execution logic: The method of performing a join in the current code block (such as the main method) threads A,

Then when the code block (main) perform a join method, it stops down continues until the thread A is finished,

The main method will continue downward.

Code One:

package com.thread;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;
import java.util.stream.IntStream;

public class ThreadJoin {
    public static void main(String[] args) throws InterruptedException {
        Thread thread1=new Thread(new Runnable() {
            @Override
            public void run() {
                for(int i=0;i<5;i++){
                    System.out.println(Thread.currentThread().getName()+":"+i);
                }
            }
        },"线程1");
        Thread thread2=new Thread(new Runnable() {
            @Override
            public void run() {
                for(int i=0;i<5;i++){
                    System.out.println(Thread.currentThread().getName()+":"+i);
                }
            }
        },"线程2");

        //启动线程1
        thread1.start ();
         // start threads 2 
        thread2.start (); 

        / ** 
         * When the code performing this step, the program is not executed down, waiting to be executed after thread1 finished down 
         * and simultaneously thread1 thread2 each earn more cpu scheduling resources 
         * / 
        thread1.join (); 
        / ** 
         * when the code performing this step, the program is not executed down, waiting for execution is finished down after thread2 
         * because at this time has already been implemented thread1 the only execution Thread2 
         * / 
        thread2.join (); 
        
        for ( int I = 0; I <2; I ++ ) { 
            System.out.println (. Thread.currentThread () getName () + "#" + I) ; 
            TimeUnit.SECONDS.sleep ( . 1 ); 
        } 
    } 

}

Use Cases:

If there is now a long-distance ticket sales site, you want to query Hefei to Shanghai Automotive;

And there are a lot of bus station in Hefei (Hefei Station 1, Station 2 Hefei, Hefei 3 stops), while Shanghai there are a lot of bus station (Shanghai Station 1, Station 2 Shanghai, Shanghai 3 stops).

This time web platform Hefei need to call each car platform interface to obtain official ticket information Hefei to Shanghai.

Normal synchronization thinking should be like this:

1. The first call Hefei 1 station to station ticket information Shanghai, time-consuming 3s,

2. Call the ticket information to Hefei Station 2 Shanghai Station, time-consuming 5s,

3. Call Hefei 3 stops to ticket information Shanghai station, time-consuming 1s.

Once these three interface call is complete, return parameter display assembly to the interface.

join method with the thread you can let go of these three interfaces simultaneously request interface, parameters, and display assembly to the customer interface call is completed and then three.

This is the total return depends on the slowest time consuming interface data that one interface.

Platform.java station class

package com.thread.car;

import java.util.ArrayList;
import java.util.List;

/**
 * 合肥车站1
 */
public class PlatForm {

    private String  startCity;
    private String  endCity;

    public PlatForm(String startCity,String endCity) {
        this.startCity=startCity;
        this.endCity=endCity;
    }

    private final List<String> list=new ArrayList<>();

    public void search(){
        this.list.add(the this .startCity + "stand" + the this .endCity + "station information 1" );
         the this .list.add ( the this .startCity + "stand" + the this .endCity + "station information 2" );
         the this .list.add ( the this .startCity + "stand" + the this .endCity + "information station 3" ); 
    } 
    public List <String> GET () {
         return  the this .list; 
    } 
}

Test.java test class

package com.thread.car;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;

public class Test {

    /*private static List<String> resList=new ArrayList<>();
    private static synchronized void addList(List<String> list){
        resList.addAll(list);
    }*/

    public static void main(String[] args) throws InterruptedException {
        //List<String> list=new ArrayList<String>();
        PlatForm pf1=new PlatForm("合肥1","上海");
        PlatForm pf2=new PlatForm("合肥2","上海");
        PlatForm pf3=new PlatForm("合肥3","上海");

        Thread thread1=new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    pf1.search();
                    TimeUnit.SECONDS.sleep(5);
                    System.out.println(Thread.currentThread().getName()+"执行结束");
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        },"合肥1站");
        Thread thread2=new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    pf2.search();
                    TimeUnit.SECONDS.sleep(4);
                    System.out.println(Thread.currentThread().getName()+"执行结束");
                } catch (InterruptedException e) {
                    e.printStackTrace();
                } 
            } 
        }, "Hefei station 2" );
        Thread thread3 = new new the Thread ( new new the Runnable () { 
            @Override 
            public  void RUN () {
                 the try { 
                    pf3.search (); 
                    TimeUnit.SECONDS.sleep ( . 1 ); 
                    System.out.println (. Thread.currentThread () getName () + "execution finished" ); 
                } the catch (InterruptedException E) { 
                    e.printStackTrace (); 
                } 
            } 
        }, "3 Hefei station");

        thread1.start();
        thread2.start();
        thread3.start();

        long start=System.currentTimeMillis();
        thread1.join();
        thread2.join();
        thread3.join();
        long end=System.currentTimeMillis();
        System.out.println((end-start)/1000);
        List<String> list=new ArrayList<String>();
        list.addAll(pf1.get());
        list.addAll(pf2.get());
        list.addAll(pf3.get());
        for(String s:list){
            System.out.println(s);
        }

    }
}

 

Guess you like

Origin www.cnblogs.com/guoyansi19900907/p/11355402.html