方式一:使用LockSupport
package com.muluo.test1013;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.LockSupport;
import java.util.concurrent.locks.ReentrantLock;
public class TodayCodeTest {
static String str1 = "abc";
static String str2 = "123";
static Thread t1 = null;
static Thread t2 = null;
public static void main(String[] args) {
t1 = new Thread(() -> {
for (int i = 0; i < str2.length(); i++) {
System.out.println(Thread.currentThread().getName() + " " + str2.charAt(i));
LockSupport.unpark(t2); // 唤醒线程
LockSupport.park(); // 挂起当前线程
}
}, "t1");
t2 = new Thread(() -> {
for (int i = 0; i < str1.length(); i++) {
LockSupport.park();
System.out.println(Thread.currentThread().getName() + " " + str1.charAt(i));
LockSupport.unpark(t1);
}
}, "t2");
t1.start();
t2.start();
}
方式二 wait/notify/ 标志位flag
package com.muluo.test1013;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.LockSupport;
import java.util.concurrent.locks.ReentrantLock;
public class TodayCodeTest {
static String str1 = "abc";
static String str2 = "123";
boolean flag = false;
public static void main(String[] args) {
TodayCodeTest todayCodeTest = new TodayCodeTest();
new Thread(() -> {
todayCodeTest.print123();
}, "t1");
new Thread(() -> {
todayCodeTest.printabc1();
}, "t2");
}
public synchronized void printabc1() {
for (int i = 0; i < str1.length(); i++) {
try {
while (flag) {
wait();
}
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName() + str1.charAt(i));
notify();
flag = true;
}
}
public synchronized void print123() {
for (int i = 0; i < str2.length(); i++) {
try {
while (!flag) {
wait();
}
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName() + str2.charAt(i));
notify();
flag = false;
}
}
方式三 使用Object的notify和wait
package com.muluo.test1013;
import java.util.concurrent.locks.LockSupport;
public class NotifyAndWaitAndObject {
public static void main(String[] args) {
String str1 = "abc";
String str2 = "123";
Object obj = new Object();
new Thread(() -> {
synchronized(obj) {
for (int i = 0; i < str2.length(); i++) {
System.out.println(Thread.currentThread().getName() + " " + str2.charAt(i));
obj.notify();
try {
obj.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}, "t1").start();
new Thread(() -> {
synchronized(obj) {
for (int i = 0; i < str1.length(); i++) {
System.out.println(Thread.currentThread().getName() + " " + str1.charAt(i));
obj.notify();
try {
obj.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}, "t2").start();
}
}
方式四
package com.muluo.test1013;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
public class ReentrantLockAndCondition {
public static void main(String[] args) {
// 1a+2b+3c+
String str1 = "123";
String str2 = "abc";
String str3 = "+++";
Lock lock = new ReentrantLock();
Condition con1 = lock.newCondition();
Condition con2 = lock.newCondition();
Condition con3 = lock.newCondition();
new Thread(() -> {
lock.lock();
for (int i = 0; i < str1.length(); i++) {
System.out.println(Thread.currentThread().getName() + " " + str1.charAt(i));
try {
con2.signal(); // 唤醒
con1.await();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
lock.unlock();
}, "t1").start();
new Thread(() -> {
lock.lock();
for (int i = 0; i < str2.length(); i++) {
System.out.println(Thread.currentThread().getName() + " " + str2.charAt(i));
try {
con3.signal();
con2.await();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
lock.unlock();
}, "t2").start();
new Thread(() -> {
lock.lock();
for (int i = 0; i < str3.length(); i++) {
System.out.println(Thread.currentThread().getName() + " " + str3.charAt(i));
try {
con1.signal();
con3.await();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
lock.unlock();
}, "t3").start();
}
}