博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Unsafe类park,unpark详解
阅读量:4606 次
发布时间:2019-06-09

本文共 2810 字,大约阅读时间需要 9 分钟。

/**

* Unblock the given thread blocked on <tt>park</tt>, or, if it is
* not blocked, cause the subsequent call to <tt>park</tt> not to
* block. Note: this operation is "unsafe" solely because the
* caller must somehow ensure that the thread has not been
* destroyed. Nothing special is usually required to ensure this
* when called from Java (in which there will ordinarily be a live
* reference to the thread) but this is not nearly-automatically
* so when calling from native code.
* @param thread the thread to unpark.
*
*/
public native void unpark(Object thread);

park方法用于Block current thread。在以下情况下返回

1 针对当前线程已经调用过unpark(多次调用unpark的效果和调用一次unpark的效果一样)

 

public static void main(String[] args) throws Exception {              Unsafe unsafe = UnsafeUtil.getUnsafe();              Thread currThread = Thread.currentThread();            unsafe.unpark(currThread);      unsafe.unpark(currThread);      unsafe.unpark(currThread);            unsafe.park(false, 0);            System.out.println("SUCCESS!!!");    }

 

park方法马上返回

2 在当前线程中断的时候或者调用unpark的时候

  

public static void main(String[] args) throws Exception {              Unsafe unsafe = UnsafeUtil.getUnsafe();              Thread currThread = Thread.currentThread();      new Thread(()->{          try {            Thread.sleep(3000);            currThread.interrupt();            //unsafe.unpark(currThread);        } catch (Exception e) {}      }).start();            unsafe.park(false, 0);            System.out.println("SUCCESS!!!");    }

 

 park在3秒后返回

3 如果是相对时间也就是isAbsolute为false(注意这里后面的单位纳秒)到期的时候

public static void main(String[] args) throws Exception {              Unsafe unsafe = UnsafeUtil.getUnsafe();      //相对时间后面的参数单位是纳秒      unsafe.park(false, 3000000000l);            System.out.println("SUCCESS!!!");    }

 

 3秒后 park返回

4 如果是绝对时间也就是isAbsolute为true(注意后面的单位是毫秒)到期的时候

public static void main(String[] args) throws Exception {              Unsafe unsafe = UnsafeUtil.getUnsafe();            long time = System.currentTimeMillis()+3000;            //绝对时间后面的参数单位是毫秒      unsafe.park(true, time);            System.out.println("SUCCESS!!!");    }

3秒后 park返回

5 无理由的返回

 

unpark方法最好不要在调用park前对当前线程调用unpark

/**     * Unblock the given thread blocked on park, or, if it is     * not blocked, cause the subsequent call to park not to     * block.  Note: this operation is "unsafe" solely because the     * caller must somehow ensure that the thread has not been     * destroyed. Nothing special is usually required to ensure this     * when called from Java (in which there will ordinarily be a live     * reference to the thread) but this is not nearly-automatically     * so when calling from native code.     * @param thread the thread to unpark.     *     */    public native void unpark(Object thread);

 

转载于:https://www.cnblogs.com/guojunwei/p/6401544.html

你可能感兴趣的文章
发送邮件
查看>>
VirtulBox虚拟机搭建Linux Centos系统
查看>>
djang-异步——定时操作
查看>>
创建型模式(二):AbstractFactory ( 抽象工厂 )
查看>>
Supporting Multiple iOS Versions and Devices
查看>>
vue 使用 element ui动态添加表单
查看>>
在JavaScript中,为什么我们在用定时器控制某一元素移动时,有时会出现元素越走越快的现象...
查看>>
2019.04.06 电商06 分页
查看>>
12.8 定位属性
查看>>
Kotlin中三元运算符
查看>>
Fragment处理接口回调,网络请求数据
查看>>
2. XHTML_WHY
查看>>
树梅派安装raspbian
查看>>
Git In Five Minutes (zz)
查看>>
Sysinternals
查看>>
assert函数
查看>>
label中添加图片
查看>>
float类型保存两位小数
查看>>
WPF动画之关键帧动画(2)
查看>>
poj 1837 Balance
查看>>