zoukankan      html  css  js  c++  java
  • Thread.join详解

    /**
     * 如果某个线程在另一个线程t上调用t.join;那么此线程将被挂起,直到目标t线程的结束才恢复即t.isAlive返回为假
     * 
     * @date:2018年6月27日
     * @author:zhangfs
     * 
     * 
     */
    public class ClientDemo {
    
        public static void main(String[] args) {
            SleepThread sleepThread1 = new SleepThread("sleepThread1", 1500),
                    sleepThread2 = new SleepThread("sleepThread2", 1500);
    
            JoinThread joinThread1 = new JoinThread("joinThread1", sleepThread1),
                    joinThread2 = new JoinThread("joinThread2", sleepThread2);
    
            sleepThread2.interrupt();
    
        }
    }
    
    

    public class SleepThread extends Thread {

    
    

    private int duration;

    
    

     

    
    

    public SleepThread(String name, int sleepTime) {

    
    

    super(name);

    
    

    this.duration = sleepTime;

    
    

    start();

    
    

    }

    
    

     

    
    

    @Override

    
    

    public void run() {

    
    

    // TODO Auto-generated method stub

    
    

    // super.run();

    
    

    try {

    
    

    sleep(duration);

    
    

    } catch (InterruptedException e) {

    
    

    // TODO Auto-generated catch block

    
    

    // e.printStackTrace();

    
    

    System.out.println(getName() + " was interrupted. is interrupted is :" + isInterrupted());

    
    

    return;

    
    

    }

    
    

    System.out.println(getName() + " has awakened");

    
    

    }

    
    

    }

     

     

    public class JoinThread extends Thread {

     

    private SleepThread sleepThread;

     

    public JoinThread(String name, SleepThread sleepThread) {

    super(name);

    this.sleepThread = sleepThread;

    start();

    }

     

    @Override

    public void run() {

    // TODO Auto-generated method stub

    try {

    sleepThread.join();

    } catch (InterruptedException e) {

    // TODO Auto-generated catch block

    // e.printStackTrace();

    System.out.println("interrupted ");

    }

    System.out.println(getName() + "  Join Complete");

    }

    }


    output:
    
    

    sleepThread2 was interrupted. is interrupted is :false

    
    

    joinThread2  Join Complete

    
    

    sleepThread1 has awakened

    
    

    joinThread1  Join Complete

     
  • 相关阅读:
    第9.11节 Python中IO模块文件打开读写操作实例
    第9.10节 Python中IO模块其他文件操作属性和方法简介
    Python使用import导入模块时执行了模块的文件但报ModuleNotFoundError错误的愚蠢问题
    Python使用import导入模块时报ValueError: source code string cannot contain null bytes的解决方案
    第9.9节 Python文件随机读写定位操作方法seek
    第9.8节 Python使用writelines函数写入文件内容
    第9.7节 Python使用write函数写入文件内容
    Python中import模块时报SyntaxError: (unicode error)utf-8 codec can not decode 错误的解决办法
    The 2nd tip of DB Query Analyzer
    水仙花数优化问题:穷举法、查找表法、组合数学法
  • 原文地址:https://www.cnblogs.com/zhangfengshi/p/9234953.html
Copyright © 2011-2022 走看看