3.1.7 线程阻塞工具类:LockSupport

Posted anxbb

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了3.1.7 线程阻塞工具类:LockSupport相关的知识,希望对你有一定的参考价值。

package 第三章.线程阻塞工具LockSupport;

import java.util.concurrent.locks.LockSupport;

/**
* Created by zzq on 2018/2/2.
*/
public class LockSupportDemo {
public static Object u = new Object();
static ChangeObjectThread t1 = new ChangeObjectThread("t1");
static ChangeObjectThread t2 = new ChangeObjectThread("t2");

public static class ChangeObjectThread extends Thread {
public ChangeObjectThread(String name) {
super.setName(name);
}

public void run() {
synchronized (u) {
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("in " + getName());
LockSupport.park();
System.out.println("out " + getName());
}
}
}


public static void main(String args[]) throws InterruptedException {
t1.start();
Thread.sleep(3000);
t2.start();
LockSupport.unpark(t1);
LockSupport.unpark(t2);
t1.join();
t2.join();
}
}









































以上是关于3.1.7 线程阻塞工具类:LockSupport的主要内容,如果未能解决你的问题,请参考以下文章

线程阻塞工具类:LockSupport(读书笔记)

挂起(suspend)与线程阻塞工具类LockSupport

LockSupport 线程工具类有啥用?

线程阻塞工具:LockSupport

Java并发:挂起与唤醒线程LockSupport工具类详解

LockSupport 工具相关整理