原创转载请注明出处:http://agilestyle.iteye.com/blog/2342923
acquireUninterruptibly()作用是使等待进入acquire()方法的线程,不允许被中断。
acquireUninterruptibly()还有重载的方法acquireUninterruptibly(int permits),此方法的作用是在等待permits的情况下不允许被中断,如果成功获得锁,则取得指定permits的个数。
Service.java
package org.fool.java.concurrent.semaphore.acquireuninterruptibly; 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++) { new String(); Math.random(); } System.out.println(Thread.currentThread().getName() + " end timer=" + System.currentTimeMillis()); semaphore.release(); } catch (Exception e) { System.out.println(Thread.currentThread().getName() + " in catch..."); e.printStackTrace(); } } }
ThreadA.java
package org.fool.java.concurrent.semaphore.acquireuninterruptibly; public class ThreadA implements Runnable { private Service service; public ThreadA(Service service) { this.service = service; } @Override public void run() { service.testMethod(); } }
ThreadB.java
package org.fool.java.concurrent.semaphore.acquireuninterruptibly; public class ThreadB implements Runnable { private Service service; public ThreadB(Service service) { this.service = service; } @Override public void run() { service.testMethod(); } }
SemaphoreTest.java
package org.fool.java.concurrent.semaphore.acquireuninterruptibly; public class SemaphoreTest { public static void main(String[] args) throws InterruptedException { Service service = new Service(); Thread a = new Thread(new ThreadA(service)); a.setName("A"); a.start(); Thread b = new Thread(new ThreadB(service)); b.setName("B"); b.start(); Thread.sleep(1000); b.interrupt(); } }
Run
Note:
线程B被成功中断。
修改Service.java,将
semaphore.acquire();
改为
semaphore.acquireUninterruptibly();
再Run
Note:
线程B没有被中断。
相关推荐
JavaEE源代码 concurrent-1.3.2JavaEE源代码 concurrent-1.3.2JavaEE源代码 concurrent-1.3.2JavaEE源代码 concurrent-1.3.2JavaEE源代码 concurrent-1.3.2JavaEE源代码 concurrent-1.3.2JavaEE源代码 concurrent-...
- copy %AXIS2_HOME%\lib\backport-util-concurrent-3.1.jar 到%ECLIPSE_HOME%\plugins\Axis2_Codegen_Wizard_1.3.0\lib - 注册此 jar 包: 編輯 %ECLIPSE_HOME%\plugins\Axis2_Codegen_Wizard_1.3.0\plugin.xml , ...
concurrent-1.3.4.jar
backport-util-concurrent-3.1.jar 和 geronimo-stax-api_1.0_spec-1.0.1.jar 复制到 MyEclipse 6.5\eclipse\plugins\Axis2_Codegen_Wizard_1.3.0\lib 文件夹下。 (3).注册此 jar 包: 修改MyEclipse 6.5\eclipse...
《并发编程库 concurrent-1.3.4-sources.jar 深度解析》 在Java编程领域,"concurrent"一词通常与多线程和并发处理相关,它指的是能够同时执行多个任务的能力。这里提到的`concurrent-1.3.4-sources.jar`是一个特定...
官方版本,亲测可用
"backport-util-concurrent-3.1.jar" 文件到 Axis2_Codegen_Wizard_1.3.0 的 lib 目录中 , 同时修改 Axis2_Codegen_Wizard_1.3.0 下的 plugin.xml 文件 , 在 <runtime> 中添加 <library name="lib/geronimo-stax-...
Concurrent-Utils Utilities for Java concurrent library. This is a library contains some useful and smart utility class for Java concurrent library. Shelly, HermesEventBus and AndroidDataStorage are ...
concurrent-1.3.2.ja
- `Semaphore`:信号量,用于限制同时访问特定资源的线程数量。 2. **同步机制** - `Synchronized`:关键字确保同一时间只有一个线程能访问特定代码块,防止数据不一致。 - `实例方法同步`:对实例方法应用...
`atlassian-util-concurrent-0.0.12.jar.zip`是这个库的压缩文件,包含了`atlassian-util-concurrent-0.0.12.jar`本身以及相关的许可证文件`atlassian-profiling-license.txt`。这个库是Atlassian为Java开发者提供的...
backport-util-concurrent-1.0.jar,
patterns-for-concurrent-and-networked-objects