locksupport是用来创建锁和其他同步类的基本线程阻塞原语。
locksupport中的park() 和 unpark() 的作用分别是阻塞线程和解除阻塞线程,而且park()和unpark()不会遇到“thread.suspend 和 thread.resume所可能引发的死锁”问题。
因为park() 和 unpark()有许可的存在;调用 park() 的线程和另一个试图将其 unpark() 的线程之间的竞争将保持活性。
基本用法
locksupport 很类似于二元信号量(只有1个许可证可供使用),如果这个许可还没有被占用,当前线程获取许可并继 续 执行;如果许可已经被占用,当前线 程阻塞,等待获取许可。
public static void main(string[] args)
{
locksupport.park();
system.out.println("block.");
}
运行该代码,可以发现主线程一直处于阻塞状态。因为 许可默认是被占用的 ,调用park()时获取不到许可,所以进入阻塞状态。
如下代码:先释放许可,再获取许可,主线程能够正常终止。locksupport许可的获取和释放,一般来说是对应的,如果多次unpark,只有一次park也不会出现什么问题,结果是许可处于可用状态。
public static void main(string[] args)
{
thread thread = thread.currentthread();
locksupport.unpark(thread);//释放许可
locksupport.park();// 获取许可
system.out.println("b");
}
locksupport是可不重入 的,如果一个线程连续2次调用 locksupport .park(),那么该线程一定会一直阻塞下去。
public static void main(string[] args) throws exception
{
thread thread = thread.currentthread();
locksupport.unpark(thread);
system.out.println("a");
locksupport.park();
system.out.println("b");
locksupport.park();
system.out.println("c");
}
这段代码打印出a和b,不会打印c,因为第二次调用park的时候,线程无法获取许可出现死锁。
下面我们来看下locksupport对应中断的响应性
public static void t2() throws exception
{
thread t = new thread(new runnable()
{
private int count = 0;
@override
public void run()
{
long start = system.currenttimemillis();
long end = 0;
while ((end - start) <= 1000)
{
count++;
end = system.currenttimemillis();
}
system.out.println("after 1 second.count=" + count);
//等待或许许可
locksupport.park();
system.out.println("thread over." + thread.currentthread().isinterrupted());
}
});
t.start();
thread.sleep(2000);
// 中断线程
t.interrupt();
system.out.println("main over");
}
最终线程会打印出thread over.true。这说明 线程如果因为调用park而阻塞的话,能够响应中断请求(中断状态被设置成true),但是不会抛出interruptedexception 。
locksupport函数列表
// 返回提供给最近一次尚未解除阻塞的 park 方法调用的 blocker 对象,如果该调用不受阻塞,则返回 null。
static object getblocker(thread t)
// 为了线程调度,禁用当前线程,除非许可可用。
static void park()
// 为了线程调度,在许可可用之前禁用当前线程。
static void park(object blocker)
// 为了线程调度禁用当前线程,最多等待指定的等待时间,除非许可可用。
static void parknanos(long nanos)
// 为了线程调度,在许可可用前禁用当前线程,并最多等待指定的等待时间。
static void parknanos(object blocker, long nanos)
// 为了线程调度,在指定的时限前禁用当前线程,除非许可可用。
static void parkuntil(long deadline)
// 为了线程调度,在指定的时限前禁用当前线程,除非许可可用。
static void parkuntil(object blocker, long deadline)
// 如果给定线程的许可尚不可用,则使其可用。
static void unpark(thread thread)
locksupport示例
对比下面的“示例1”和“示例2”可以更清晰的了解locksupport的用法。
示例1
public class waittest1 {
public static void main(string[] args) {
threada ta = new threada("ta");
synchronized(ta) { // 通过synchronized(ta)获取“对象ta的同步锁”
try {
system.out.println(thread.currentthread().getname()+" start ta");
ta.start();
system.out.println(thread.currentthread().getname()+" block");
// 主线程等待
ta.wait();
system.out.println(thread.currentthread().getname()+" continue");
} catch (interruptedexception e) {
e.printstacktrace();
}
}
}
static class threada extends thread{
public threada(string name) {
super(name);
}
public void run() {
synchronized (this) { // 通过synchronized(this)获取“当前对象的同步锁”
system.out.println(thread.currentthread().getname()+" wakup others");
notify(); // 唤醒“当前对象上的等待线程”
}
}
}
}
示例2
import java.util.concurrent.locks.locksupport;
public class locksupporttest1 {
private static thread mainthread;
public static void main(string[] args) {
threada ta = new threada("ta");
// 获取主线程
mainthread = thread.currentthread();
system.out.println(thread.currentthread().getname()+" start ta");
ta.start();
system.out.println(thread.currentthread().getname()+" block");
// 主线程阻塞
locksupport.park(mainthread);
system.out.println(thread.currentthread().getname()+" continue");
}
static class threada extends thread{
public threada(string name) {
super(name);
}
public void run() {
system.out.println(thread.currentthread().getname()+" wakup others");
// 唤醒“主线程”
locksupport.unpark(mainthread);
}
}
}
运行结果:
main start ta
main block
ta wakup others
main continue
说明:park和wait的区别。wait让线程阻塞前,必须通过synchronized获取同步锁。
更多详解java多线程编程中locksupport类的线程阻塞用法。