假设有一段字符串“1A2B3C4D”, 启动两个线程,线程交替打印字符串中的某个字符
方法一:使用wait、notify的方式实现
public class ThreadCommunication {
public static String str = "1A2B3C4";
public static Object monitor = new Object();
public static boolean running = true;
public static int sept = -1;
public static void main(String[] args) {
char[] chars = str.toCharArray();
new Thread(() -> {
while (true) {
synchronized (monitor) {
if(running) {
int index = getIndex(chars.length);
System.out.print(Thread.currentThread().getName() + " : " );
System.out.println(chars[index]);
running = false;
SleepUtil.sleep(1);
monitor.notifyAll();
} else {
try {
monitor.wait();
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
}
}
}).start();
new Thread(() -> {
while (true) {
synchronized (monitor) {
if(!running) {
int index = getIndex(chars.length);
System.out.print(Thread.currentThread().getName() + " : " );
System.out.println(chars[index]);
running = true;
SleepUtil.sleep(1);
monitor.notifyAll();
} else {
try {
monitor.wait();
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
}
}
}).start();
}
public static int getIndex(int size) {
sept ++;
if(sept == size) {
sept = 0;
}
return sept;
}
}
方法二:使用LockSupport工具类实现
import java.util.concurrent.locks.LockSupport;
public class ThreadCommunication2 {
public static String str = "1A2B3C4";
public static int sept = -1;
public static Thread t1, t2;
public static void main(String[] args) {
char[] chars = str.toCharArray();
t1 = new Thread(() -> {
while (true) {
int index = getIndex(chars.length);
System.out.print(Thread.currentThread().getName() + " : " );
System.out.println(chars[index]);
SleepUtil.sleep(1);
LockSupport.unpark(t2);
LockSupport.park();
}
}, "T1");
t2 = new Thread(() -> {
while (true) {
LockSupport.park();
int index = getIndex(chars.length);
System.out.print(Thread.currentThread().getName() + " : " );
System.out.println(chars[index]);
SleepUtil.sleep(1);
LockSupport.unpark(t1);
}
}, "T2");
t1.start();
t2.start();
}
public static int getIndex(int size) {
sept ++;
if(sept == size) {
sept = 0;
}
return sept;
}
}
方法三:使用ReentrantLock、Condition实现
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.ReentrantLock;
public class ThreadCommunication3 {
public static String str = "1A2B3C4";
public static int sept = -1;
public static ReentrantLock lock = new ReentrantLock();
public static Condition h = lock.newCondition();
public static Condition d = lock.newCondition();
public static void h(char[] chars, int index) throws Exception {
lock.lock();
try {
System.out.print(Thread.currentThread().getName() + " : " );
System.out.println(chars[index]);
SleepUtil.sleep(1);
d.signal();
h.await();
} finally {
lock.unlock();
}
}
public static void d(char[] chars, int index) throws Exception {
lock.lock();
try {
System.out.print(Thread.currentThread().getName() + " : " );
System.out.println(chars[index]);
SleepUtil.sleep(1);
h.signal();
d.await();
} finally {
lock.unlock();
}
}
public static void main(String[] args) {
char[] chars = str.toCharArray();
new Thread(() -> {
while (true) {
int index = getIndex(chars.length);
try {
h(chars, index);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
}).start();
new Thread(() -> {
while (true) {
int index = getIndex(chars.length);
try {
d(chars, index);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
}).start();
}
public static int getIndex(int size) {
sept ++;
if(sept == size) {
sept = 0;
}
return sept;
}
}
以上三种方法,是目前我想到的。