【多线程】interrupt、interrupted、isInterrupted方法

package cn.hncu.lang.thread_;

/*
 * Thread类成员方法:interrupt、interrupted、isInterrupted
 */
public class ThreadMethod_ {
    public static void main(String[] args) {
        OneThre thread = new OneThre();
        thread.start();

        //中断当前对象thread所代表的线程
        thread.interrupt();
        System.out.println("main:thread.isInterrupted() = "+thread.isInterrupted());
        System.out.println("main:thread.interrupted() = "+thread.interrupted());
        System.out.println("main:Thread.currentThread().isInterrupted() = "
                                +Thread.currentThread().isInterrupted());
        System.out.println("main:Thread.interrupted() = "+Thread.interrupted());

        //中断当前的线程Main线程
        Thread.currentThread().interrupt();
        System.out.println("main:thread.isInterrupted() = "+thread.isInterrupted());
        System.out.println("main:thread.interrupted() = "+thread.interrupted());
        System.out.println("main:Thread.currentThread().isInterrupted() = "
                                +Thread.currentThread().isInterrupted());
        System.out.println("main:Thread.interrupted() = "+Thread.interrupted());

    }

}
/*
 * 关于interrupt()方法的说明:
 * 1.首先interrupt方法的使用效果不像for+break语句那样,马上就停止循环。
 * 2.调用interrupt方法仅仅是在当前线程中打了一个停止的标记,并不是真正的停止线程。
 * 3.this.interrupt()指的是:中断当前对象所代表的线程。
 * 4.Thread.currentThread().interrupt()指的是:中断当前对象所在的线程。
 * 
 * 关于isInterrupted()方法的说明:
 * 1.非静态方法,测试线程是否已经中断,此线程就是当前对象所代表的线程。
 * 2.线程的中断状态不受该方法的影响。
 * 3.this.isInterrupted()指的是:测试当前对象所代表的线程是否已经中断。
 * 4.Thread.currentThread().isInterrupted()指的是:测试当前对象所在的线程是否已经中断。
 * 
 * 关于interrupted()方法的说明:
 * 1.静态方法,测试当前线程是否已经被中断,当前线程中断状态会被该方法清除。
 * 2.如果连续两次调用该方法,则第二次调用将返回false。
 * 
 */
class OneThre extends Thread{
    @Override
    public void run() {
        for(int i=0;i<5;i++){
            System.out.println(i);

            this.interrupt();
            System.out.println("run:this.isInterrupted() = "+this.isInterrupted());
            System.out.println("run:Thread.currentThread().isInterrupted() = "
                                    +Thread.currentThread().isInterrupted());
            System.out.println("run:this.interrupted() = "+this.interrupted());
            System.out.println(OneThre.interrupted());
            Thread.currentThread().interrupt();
            System.out.println(Thread.interrupted());

        }
    }
}
main:thread.isInterrupted() = true
0
main:thread.interrupted() = false
run:this.isInterrupted() = true
main:Thread.currentThread().isInterrupted() = false
run:Thread.currentThread().isInterrupted() = true
main:Thread.interrupted() = false
run:this.interrupted() = true
main:thread.isInterrupted() = false
false
main:thread.interrupted() = true
true
1
main:Thread.currentThread().isInterrupted() = false
run:this.isInterrupted() = true
main:Thread.interrupted() = false
run:Thread.currentThread().isInterrupted() = true
run:this.interrupted() = true
false
true
2
run:this.isInterrupted() = true
run:Thread.currentThread().isInterrupted() = true
run:this.interrupted() = true
false
true
3
run:this.isInterrupted() = true
run:Thread.currentThread().isInterrupted() = true
run:this.interrupted() = true
false
true
4
run:this.isInterrupted() = true
run:Thread.currentThread().isInterrupted() = true
run:this.interrupted() = true
false
true

currentThread方法:

/**
 * 项目名:线程中的常用方法
 * 时间 :2017-9-19 上午8:26:55
 */
/*
 * 【currentThread()方法】:
 * 1.返回对当前正在执行的线程对象的引用。 
 * 2.当前执行的线程。
 */
class CurrentThread extends Thread{
    public CurrentThread() {
        System.out.println("构造方法11 : "+currentThread().getName());
        System.out.println("构造方法22 : "+this.currentThread().getName());
    }
    @Override
    public void run() {
        System.out.println("run方法33 : "+currentThread().getName());
        System.out.println("run方法44 : "+this.currentThread().getName());
    }
    public static void main(String[] args) {
        CurrentThread thread = new CurrentThread();
        //thread.start();
        thread.run();
        System.out.println("main方法55 : "+currentThread().getName());
        System.out.println("main方法66 : "+thread.currentThread().getName());
        /*      
        【执行结果】:
            构造方法11 : main
            构造方法22 : main
            main方法55 : main
            main方法66 : main
            run方法33 : Thread-0
            run方法44 : Thread-0
        【说明】:
        1.根据运行结果可以看出,currentThread()返回对当前正在执行的线程对象的引用。
        2.首先在main方法中currentThread()方法代表的是当前正在执行的线程对象:
          main方法的当前执行线程只有main线程,main线程是由JVM执行的。因此当前
            线程main线程对象的名字为main。
        3.构造方法是在类加载的时候才开始初始化的,即是由JVM执行的,因此当前正在执行的
             线程对象的引用是main线程。this就是指当前对象,此时当前对象就是虚拟机中执行
             的对象。
        4.当thread对象调用start方法启动线程时 ,run方法开始运行即thread线程开始
             运行,因此当前的线程对象就是thread对象,因此当前线程的名字会是Thread-0。
        5.但是如果直接运行run方法,当前只有一个线程main线程在运行,因此输出的结果全是main。 
        */
    }
}
  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值