• 从头认识java-18.2 主要的线程机制(7)join


    这一章节我们来讨论一下join。

    1.什么是join?

    阻塞当前线程,让.join的线程先做完。然后再继续当前线程

    以下是api的解释:

    A.join,在API中的解释是,阻塞当前线程B,直到A运行完毕并死掉,再运行B。

     


    2.样例:

    package com.ray.ch17;
    
    public class Test {
    
    	public static void main(String[] args) throws InterruptedException {
    		System.out.println(Thread.currentThread().getName() + " start");
    		SleepThread sleepThread = new SleepThread("sleep");
    		Thread.sleep(2000);
    		JoinThread joinThread = new JoinThread(sleepThread, "join");
    //		joinThread.join();
    		System.out.println(Thread.currentThread().getName() + " end");
    	}
    }
    
    class SleepThread extends Thread {
    
    	public SleepThread(String name) {
    		super(name);
    		start();
    	}
    
    	@Override
    	public void run() {
    		try {
    			for (int i = 0; i < 5; i++) {
    				System.out.println(getName() + " SleepThread");
    				Thread.sleep(1000);
    			}
    		} catch (InterruptedException e) {
    			e.printStackTrace();
    		}
    	}
    }
    
    class JoinThread extends Thread {
    
    	private SleepThread sleepThread;
    
    	public JoinThread(SleepThread sleepThread, String name) {
    		super(name);
    		this.sleepThread = sleepThread;
    		start();
    	}
    
    	@Override
    	public void run() {
    		try {
    			System.out.println(getName() + " join start");
    			sleepThread.join();
    			System.out.println(getName() + " join end");
    		} catch (InterruptedException e) {
    			e.printStackTrace();
    		}
    	}
    }

    输出:

    main start
    sleep SleepThread
    sleep SleepThread
    sleep SleepThread
    main end
    join join start
    sleep SleepThread
    sleep SleepThread
    join join end


    package com.ray.ch17;
    
    public class Test {
    
    	public static void main(String[] args) throws InterruptedException {
    		System.out.println(Thread.currentThread().getName() + " start");
    		SleepThread sleepThread = new SleepThread("sleep");
    		Thread.sleep(2000);
    		JoinThread joinThread = new JoinThread(sleepThread, "join");
    		joinThread.join();
    		System.out.println(Thread.currentThread().getName() + " end");
    	}
    }
    
    class SleepThread extends Thread {
    
    	public SleepThread(String name) {
    		super(name);
    		start();
    	}
    
    	@Override
    	public void run() {
    		try {
    			for (int i = 0; i < 5; i++) {
    				System.out.println(getName() + " SleepThread");
    				Thread.sleep(1000);
    			}
    		} catch (InterruptedException e) {
    			e.printStackTrace();
    		}
    	}
    }
    
    class JoinThread extends Thread {
    
    	private SleepThread sleepThread;
    
    	public JoinThread(SleepThread sleepThread, String name) {
    		super(name);
    		this.sleepThread = sleepThread;
    		start();
    	}
    
    	@Override
    	public void run() {
    		try {
    			System.out.println(getName() + " join start");
    			sleepThread.join();
    			System.out.println(getName() + " join end");
    		} catch (InterruptedException e) {
    			e.printStackTrace();
    		}
    	}
    }

    输出:

    main start
    sleep SleepThread
    sleep SleepThread
    sleep SleepThread
    join join start
    sleep SleepThread
    sleep SleepThread
    join join end
    main end


    上面两个代码的不同主要是体如今joinThread.join();是否被凝视。

    对照上面的两个代码的输出,主要不同在于main end 的位置。当joinThread.join();的时候,因为须要先阻塞main方法的线程,先完毕joinThread所在线程,因此main end的位置会出现上面的变化。

    还有一方面。我们从sleep和join两个线程输出的文字的位置也能够推导出上面的结论。


    总结:这一章节我们简介了一下join。


    这一章节就到这里,谢谢。

    -----------------------------------

    文件夹


  • 相关阅读:
    重构引发的开发思考
    JS-正则表达式
    中文数字转数值
    框架-VuePress(未完)
    HTML-表格
    框架-Vue 2.*的补充
    框架-Vue Class Component tsx 支持(vue-tsx-support V2.2.0)
    CSS-flex
    框架-Vue Class Component 官方支持(vue 2.*、Vue Class Component、vue-property-decorator 9.0.2、vuex-class 0.3.2)
    vue-router:2020-03-26
  • 原文地址:https://www.cnblogs.com/tlnshuju/p/7338492.html
Copyright © 2020-2023  润新知