今天学习LockSupport工具类,该类是JUC原子包中的类,通过单元测试代码把所有public api方法跑了一遍,大致了解了底层实现,初学乍练,有很多一知半解的地方,待后续有了深入理解再来补充
package test.java.util.concurrent.locks;
import java.util.concurrent.locks.LockSupport;
import org.junit.Test;
/**
* LockSupport的测试类
*
* @author zqw
* @date 2020-06-21 18:27:51
*/
public class LockSupportTest {
public static Object object = new Object();
public static class TestThread extends Thread {
public TestThread(String name) {
super(name);
}
@Override public void run() {
LockSupport.park(object);
System.out.println("当前线程: " + getName());
if (Thread.currentThread().isInterrupted()) {
System.out.println(getName()+"被中断了");
}
System.out.println(getName()+"继续执行");
}
}
/**
* park和unPark 和wait和notify功能一样,但是不能交叉使用
* park和unPark没有顺序
* void
* @Param
* @author zhqwm
* @date 2020/6/21 19:18
*/
@Test
public void testParkAndUnpark() throws Exception {
TestThread t1 = new TestThread("t1");
TestThread t2 = new TestThread("t2");
t1.start();
Thread.sleep(1000L);
t2.start();
Thread.sleep(1000L);
t1.interrupt();
LockSupport.unpark(t2);
t1.join();
t2.join();
}
public static class TestThread1 extends Thread {
public TestThread1(String name) {
super(name);
}
@Override public void run() {
synchronized (object) {
System.out.println("当前线程: " + getName());
long start=System.nanoTime();
LockSupport.parkNanos(100000000);
System.out.println("差值:"+(System.nanoTime()-start));
if (Thread.currentThread().isInterrupted()) {
System.out.println(getName()+"被中断了");
}
System.out.println(getName()+"继续执行");
}
}
}
/**
* park和unPark 和wait和notify功能一样,但是不能交叉使用
* park和unPark没有顺序
* void
* @Param
* @author zhqwm
* @date 2020/6/21 19:18
*/
@Test
public void testParkNanos()throws Exception{
TestThread1 t1 = new TestThread1("t1");
t1.start();
Thread.sleep(200L);
LockSupport.unpark(t1);
}
public static class TestThread2 extends Thread {
public TestThread2(String name) {
super(name);
}
@Override public void run() {
synchronized (object) {
System.out.println("当前线程: " + getName());
long start=System.nanoTime();
LockSupport.parkUntil(object,1000);
System.out.println("blocker:"+LockSupport.getBlocker(this));
System.out.println("差值:"+(System.nanoTime()-start));
if (Thread.currentThread().isInterrupted()) {
System.out.println(getName()+"被中断了");
}
System.out.println(getName()+"继续执行");
}
}
}
/**
* 在指定的时限前禁用当前线程
* void
* @Param
* @author zhqwm
* @date 2020/6/21 19:18
*/
@Test
public void testParkUntil()throws Exception{
TestThread2 t1 = new TestThread2("t1");
t1.start();
// LockSupport.unpark(t1);
}
/**
* 在指定的时限前禁用当前线程
* void
* @Param
* @author zhqwm
* @date 2020/6/21 19:18
*/
@Test
public void testGetBlocker()throws Exception{
TestThread2 t1 = new TestThread2("t1");
t1.start();
LockSupport.unpark(t1);
}
public static class TestThread3 extends Thread {
public TestThread3(String name) {
super(name);
}
@Override public void run() {
synchronized (object) {
System.out.println("当前线程: " + getName());
long start=System.nanoTime();
LockSupport.park();
System.out.println("blocker:"+LockSupport.getBlocker(this));
System.out.println("差值:"+(System.nanoTime()-start));
if (Thread.currentThread().isInterrupted()) {
System.out.println(getName()+"被中断了");
}
System.out.println(getName()+"继续执行");
}
}
}
/**
* park和unPark 和wait和notify功能一样,但是不能交叉使用
* park和unPark没有顺序
* void
* @Param
* @author zhqwm
* @date 2020/6/21 19:18
*/
@Test
public void testPark()throws Exception{
TestThread3 t1 = new TestThread3("t1");
t1.start();
LockSupport.unpark(t1);
}
public static class TestThread4 extends Thread {
public TestThread4(String name) {
super(name);
}
@Override public void run() {
synchronized (object) {
System.out.println("当前线程: " + getName());
long start=System.nanoTime();
LockSupport.parkNanos(10000);
System.out.println("差值:"+(System.nanoTime()-start));
if (Thread.currentThread().isInterrupted()) {
System.out.println(getName()+"被中断了");
}
System.out.println(getName()+"继续执行");
}
}
}
/**
* park和unPark 和wait和notify功能一样,但是不能交叉使用
* park和unPark没有顺序
* void
* @Param
* @author zhqwm
* @date 2020/6/21 19:18
*/
@Test
public void testParkNanosLong()throws Exception{
TestThread4 t1 = new TestThread4("t1");
t1.start();
}
public static class TestThread5 extends Thread {
public TestThread5(String name) {
super(name);
}
@Override public void run() {
synchronized (object) {
System.out.println("当前线程: " + getName());
long start=System.nanoTime();
LockSupport.parkUntil(10000);
System.out.println("差值:"+(System.nanoTime()-start));
if (Thread.currentThread().isInterrupted()) {
System.out.println(getName()+"被中断了");
}
System.out.println(getName()+"继续执行");
}
}
}
/**
* 在指定的时限前禁用当前线程
* void
* @Param
* @author zhqwm
* @date 2020/6/21 19:18
*/
@Test
public void testParkUntilL()throws Exception{
TestThread5 t1 = new TestThread5("t1");
t1.start();
}
}