![Java并发编程:核心方法与框架](https://wfqqreader-1252317822.image.myqcloud.com/cover/235/822235/b_822235.jpg)
上QQ阅读APP看书,第一时间看更新
![](https://epubservercos.yuewen.com/D3D8F4/4410924203007901/epubprivate/OEBPS/Images/icon1.png?sign=1738854374-IbFA7yPr0RT6KqoiMeTMoFDX3exyhcaP-0-c9d95ce2eeaabdb2bfeab4ffa735ba2f)
1.1.4 方法acquireUninterruptibly()的使用
方法acquireUninterruptibly()的作用是使等待进入acquire()方法的线程,不允许被中断。
先来看一个能中断的实验。
创建项目Semaphore_acquireUninterruptibly_1,类Service.java代码如下:
package service; import java.util.concurrent.Semaphore; public class Service { private Semaphore semaphore = new Semaphore(1); public void testMethod() { try { semaphore.acquire(); System.out.println(Thread.currentThread().getName() + " begin timer=" + System.currentTimeMillis()); for (int i = 0; i < Integer.MAX_VALUE / 50; i++) { String newString = new String(); Math.random(); } System.out.println(Thread.currentThread().getName() + " end timer=" + System.currentTimeMillis()); semaphore.release(); } catch (InterruptedException e) { System.out.println("线程" + Thread.currentThread().getName() + "进入了catch"); e.printStackTrace(); } } }
线程类代码如图1-9所示。
![](https://epubservercos.yuewen.com/D3D8F4/4410924203007901/epubprivate/OEBPS/Images/figure_0021_0001.jpg?sign=1738854374-H6hp548qNQzbMDatMEBhquZuG5Ppsd25-0-51dd444b05688d188d6f384b50dd40e6)
图1-9 线程类代码
运行类Run.java代码如下:
package test; import service.Service; import extthread.ThreadA; import extthread.ThreadB; public class Run { public static void main(String[] args) throws InterruptedException { Service service = new Service(); ThreadA a = new ThreadA(service); a.setName("A"); a.start(); ThreadB b = new ThreadB(service); b.setName("B"); b.start(); Thread.sleep(1000); b.interrupt(); System.out.println("main中断了a"); } }
程序运行的效果如图1-10所示。
![](https://epubservercos.yuewen.com/D3D8F4/4410924203007901/epubprivate/OEBPS/Images/figure_0021_0002.jpg?sign=1738854374-4B4bQV8lazXD2N6M6pmU5TueHtyW29N3-0-7cb48536e89f7e6132527d57fd341ab3)
图1-10 线程B被中断
线程B成功被中断。
那么不能被中断是什么效果呢?
创建项目Semaphore_acquireUninterruptibly_2,将Semaphore_acquireUninterruptibly_1项目中的所有源代码复制到Semaphore_acquireUninterruptibly_2中,更改Service.java文件代码如下:
package service; import java.util.concurrent.Semaphore; public class Service { private Semaphore semaphore = new Semaphore(1); public void testMethod() { semaphore.acquireUninterruptibly(); System.out.println(Thread.currentThread().getName() + " begin timer=" + System.currentTimeMillis()); for (int i = 0; i < Integer.MAX_VALUE / 50; i++) { String newString = new String(); Math.random(); } System.out.println(Thread.currentThread().getName() + " end timer=" + System.currentTimeMillis()); semaphore.release(); } }
程序运行后的效果如图1-11所示。
![](https://epubservercos.yuewen.com/D3D8F4/4410924203007901/epubprivate/OEBPS/Images/figure_0022_0001.jpg?sign=1738854374-ntNaEeHjdjuFDd7deEmKfm4YGtgn1Jqt-0-ca032633c9179c8e79102fe4682758c6)
图1-11 线程B没有被中断
acquireUninterruptibly()方法还有重载的写法acquire-Uninterruptibly(int permits),此方法的作用是在等待许可的情况下不允许中断,如果成功获得锁,则取得指定的permits许可个数。