【Java并发】synchronized不可以被中断?

synchronized方法可以被中断

我们常说synchronized不可以被中断,并不指synchronized方法不可中断,比如:

public class InterruptSynMethodTest {
	public synchronized void foo() throws InterruptedException {
		System.out.println("foo begin");
		for (int i =0; i < 100; ++i) {
			System.out.println("foo ...");
			Thread.sleep(1000);
		}
	}

	public static void main(String[] args) {
		InterruptSynMethodTest it = new InterruptSynMethodTest();
		ExecutorService es = Executors.newCachedThreadPool();
		es.execute(() -> {
	        try {
	            it.foo();
	        } catch (InterruptedException e) {
	        	System.out.println("foo is interrupted, msg=" + e.getMessage());
	        }
	    });
		try {
			Thread.sleep(3000);
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		es.shutdownNow();
	    System.out.println("Main end");
	}
}

synchronized方法foo就可以被中断,执行结果为:

foo begin
foo ...
foo ...
foo ...
Main end
foo is interrupted, msg=sleep interrupted

synchronized等待不可中断

synchronized不可以被中断,指的是synchronized等待不可中断,比如:

public class InterruptTest {
	
	public synchronized void foo1() {
		System.out.println("foo1 begin");
		for (int i =0; i < 5; ++i) {
			System.out.println("foo1 ...");
			try {
				Thread.sleep(1000);
			} catch (InterruptedException e) {
				System.out.println("foo1 sleep is interrupted, msg=" + e.getMessage());
			}
		}
	}

	public synchronized void foo2() throws InterruptedException {
		System.out.println("foo2 begin");
		for (int i =0; i < 100; ++i) {
			System.out.println("foo2 ...");
			Thread.sleep(1000);
		}
	}

	public static void main(String[] args) {
		InterruptTest it = new InterruptTest();
		ExecutorService es = Executors.newCachedThreadPool();
		es.execute(() -> it.foo1());	
		es.execute(() -> {
	        try {
	            it.foo2();
	        } catch (InterruptedException e) {
	        	System.out.println("foo2 is interrupted, msg=" + e.getMessage());
	        }
	    });	
		try {
			Thread.sleep(3000);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		es.shutdownNow();
	    System.out.println("Main end");
	}
}

foo2的synchronized在等待foo1时不可被中断,只有在foo2拿到锁之后才可被中断,执行结果为:

foo1 begin
foo1 ...
foo1 ...
foo1 ...
Main end
foo1 sleep is interrupted, msg=sleep interrupted
foo1 ...
foo1 ...
foo2 begin
foo2 ...
foo2 is interrupted, msg=sleep interrupted

什么情况下可被中断?

根据Java文档,判断方法很简单:只要调用的方法抛出InterruptedException异常,那么它就可以被中断;不抛出InterruptedException的方法是不可中断的。比如如上的foo1方法就不可被中断,foo2方法可被中断。

参考:

Interrupts

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值