关于interrupt方法

interrupt()

If this thread is blocked in an invocation of the {@link

  • Object#wait() wait()}, {@link Object#wait(long) wait(long)}, or {@link
  • Object#wait(long, int) wait(long, int)} methods of the {@link Object}
  • class, or of the {@link #join()}, {@link #join(long)}, {@link
  • #join(long, int)}, {@link #sleep(long)}, or {@link #sleep(long, int)},
  • methods of this class, then its interrupt status will be cleared and it
  • will receive an {@link InterruptedException}.

翻译:
如果这个线程在调用{@link

  • Object#wait() wait()}, {@link Object#wait(long) wait(long)},或{@link

{@link Object}的wait(long, int)方法

类,或{@link #join()}, {@link #join(long)}, {@link . *类

  • #加入(长,int)}, {@link sleep#睡眠(长)}或{@link #睡眠(长,int)},

*方法,那么它的中断状态将被清除,并且它

*将收到一个{@link InterruptedException}。
用法:
在一个线程处于blocked阻塞状态去调用interrupt()方法时候,会中断线程并且使该线程直接结束(terminated)
但是当线程调用的方法是一个死循环,会清除中断后继续执行.

static String lock = "aa";

	public void test() {
		for (int i = 0; ; i++) {
			System.out.println(i);
			try {
				Thread.sleep(500);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
			if (i == 5) {
				try {
					lock.wait();
					// Thread.sleep(3000);
					System.out.println(1111);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
			}
		}
	}

	public static void main(String[] args) throws InterruptedException {
		Demo2_1 demo2_1 = new Demo2_1();

		Thread t1 = new Thread() {
			@Override
			public void run() {
				synchronized (lock) {
					demo2_1.test();
				}
			}
		};
		t1.start();
		Thread t2 = new Thread() {
			@Override
			public void run() {
				synchronized (lock) {
					try {
						Thread.sleep(1000);
					} catch (InterruptedException e) {
						e.printStackTrace();
					}
					// lock.notifyAll();
					System.out.println("ok");
				}
			}
		};
		t2.start();
		try {
			Thread.sleep(4000);
			t1.interrupt();
			System.out.println(t1.getState());//waiting伴随着阻塞
			// System.out.println(t2.getState());
		} catch (InterruptedException e) {
			e.printStackTrace();
		}

	}
static String lock = "aa";

	public void test() {

		try {
			for (int i = 0; ; i++) {
				System.out.println(i);
				Thread.sleep(500);
				if (i == 5) {
					lock.wait();
					// Thread.sleep(3000);
					System.out.println(1111);
				}
			}
		} catch (InterruptedException e) {
			e.printStackTrace();
		}

	}


## 把循环内容放入try-catch,线程就会进入死亡状态
	public static void main(String[] args) throws InterruptedException {
		Demo2_1 demo2_1 = new Demo2_1();

		Thread t1 = new Thread() {
			@Override
			public void run() {
				synchronized (lock) {
					demo2_1.test();
				}
			}
		};
		t1.start();
		Thread t2 = new Thread() {
			@Override
			public void run() {
				synchronized (lock) {
					try {
						Thread.sleep(1000);
					} catch (InterruptedException e) {
						e.printStackTrace();
					}
					// lock.notifyAll();
					System.out.println("ok");
				}
			}
		};
		t2.start();
		try {
			Thread.sleep(4000);
			t1.interrupt();
			Thread.sleep(1000);
			System.out.println(t1.getState());//waiting伴随着阻塞
			// System.out.println(t2.getState());
		} catch (InterruptedException e) {
			e.printStackTrace();
		}

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值