一、 wait(),notify() 方法
public class Test1 {
private static ArrayList<Object> lists = new ArrayList<Object>();
public void add(Object o){
lists.add(o);
}
public int size(){
return lists.size();
}
public static void main(String[] args) {
final Object lock = new Object();
Thread t1 = new Thread(new Runnable() {
public void run() {
System.out.println("t1 执行");
synchronized (lock){
for (int i = 1; i <= 10; i++) {
lists.add(i);
System.out.println("t1 add " + i);
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
if(lists.size() == 5){
// 通知 t2 执行
lock.notify();
try {
// t1 释放锁
lock.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
}
});
Thread t2 = new Thread(new Runnable() {
public void run() {
System.out.println("t2 执行");
synchronized (lock){
if(lists.size() != 5){
try {
lock.wait(); // t2 释放锁
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.println("t2 结束");
// 通知t1执行
lock.notify();
}
}
});
t2.start();
t1.start();
}
}
二、 CountDownLatch
public class TestCountDownLatch {
private static ArrayList<Object> lists = new ArrayList<Object>();
public void add(Object o){
lists.add(o);
}
public int size(){
return lists.size();
}
public static void main(String[] args) {
final CountDownLatch latch1 = new CountDownLatch(1);
final CountDownLatch latch2 = new CountDownLatch(1);
Thread t1 = new Thread(new Runnable() {
public void run() {
System.out.println("t1 执行");
for (int i = 1; i <= 10; i++) {
lists.add(i);
System.out.println("t1 add " + i);
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
if(lists.size() == 5){
latch2.countDown(); // 打开门栓
try {
latch1.await(); // 拴住
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
});
Thread t2 = new Thread(new Runnable() {
public void run() {
System.out.println("t2 执行");
if(lists.size() != 5){
try {
latch2.await(); // 拴住门栓
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.println("t2 结束");
// 打开门栓
latch1.countDown();
}
});
t2.start();
t1.start();
}
}
三、LockSupport
public class TestLockPark {
private static ArrayList<Object> lists = new ArrayList<Object>();
public void add(Object o){
lists.add(o);
}
public int size(){
return lists.size();
}
static Thread t1 = null;
static Thread t2 = null;
public static void main(String[] args) {
t1 = new Thread(new Runnable() {
public void run() {
System.out.println("t1 执行");
for (int i = 1; i <= 10; i++) {
lists.add(i);
System.out.println("t1 add " + i);
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
if(lists.size() == 5){
LockSupport.unpark(t2);
try {
LockSupport.park();
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
});
t2 = new Thread(new Runnable() {
public void run() {
System.out.println("t2 执行");
if(lists.size() != 5){
try {
LockSupport.park();
} catch (Exception e) {
e.printStackTrace();
}
}
System.out.println("t2 结束");
LockSupport.unpark(t1);
}
});
t2.start();
t1.start();
}
}
四、volatile
public class TestVolatile {
private static volatile List<Object> lists = Collections.synchronizedList(new LinkedList<Object>());
public void add(Object o){
lists.add(o);
}
public int size(){
return lists.size();
}
public static void main(String[] args) {
Thread t1 = new Thread(new Runnable() {
public void run() {
System.out.println("t1 执行");
for (int i = 1; i <= 10; i++) {
lists.add(i);
System.out.println("t1 add " + i);
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
});
Thread t2 = new Thread(new Runnable() {
public void run() {
System.out.println("t2 执行");
while (true){
if (lists.size() == 5){
break;
}
}
System.out.println("t2 结束");
}
});
t2.start();
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
t1.start();
}
}