面试题:
两个线程交替打印26个英文字母和数字
1A2B3C…26Z
方法一 park unpark
public class Thread_two_thread_06_26 {
static Thread t1;
static Thread t2;
public static void main(String[] args) {
List<Integer> numList = new ArrayList<>();
for (int i = 1; i < 27; i++) {
numList.add(i);
}
List<String> yingwenNum = new ArrayList<>();
char a[] = new char[26];
for (int i = 0; i < 26; i++) {
char c = (char) ('A' + i);
yingwenNum.add(String.valueOf(c));
}
t1 = new Thread(()->{
numList.forEach((o)->{
System.out.println(o);
LockSupport.unpark(t2);
LockSupport.park();
});
});
t2 = new Thread(()->{
yingwenNum.forEach((o)->{
LockSupport.park();
System.out.println(o);
LockSupport.unpark(t1);
});
});
t1.start();
sleep(1);
t2.start();
}
private static void sleep(Integer sleepTime){
try{
Thread.sleep(sleepTime);
}catch (Exception e){
}
}
}
方法二:synchronized wait notify
public class Thread_two_thread_07_26 {
static Thread t1;
static Thread t2;
public static void main(String[] args) {
Object obj = new Object();
List<Integer> numList = new ArrayList<>();
List<String> yingwenNum = new ArrayList<>();
get26English(numList, yingwenNum);
new Thread(()->{
yingwenNum.forEach((o)->{
try {
synchronized(obj){
obj.wait();
System.out.println(o);
obj.notify();
}
} catch (InterruptedException e) {
e.printStackTrace();
}
});
}).start();
try {
Thread.sleep(1);
} catch (InterruptedException e) {
e.printStackTrace();
}
new Thread(()->{
numList.forEach((o)->{
try {
synchronized(obj){
System.out.println(o);
obj.notify();
obj.wait();
}
} catch (InterruptedException e) {
e.printStackTrace();
}
});
}).start();
}
public static void get26English(List<Integer> numList, List<String> yingwenNum) {
for (int i = 1; i < 27; i++) {
numList.add(i);
}
for (int i = 0; i < 26; i++) {
char c = (char) ('A' + i);
yingwenNum.add(String.valueOf(c));
}
}
}
方法三:exchanger (不能完全实现交替打印)
public class Thread_two_thread_08_26 {
public static void main(String[] args) {
Exchanger<Object> exchanger = new Exchanger<>();
Thread_two_thread_07_26 th = new Thread_two_thread_07_26();
List<Integer> numList = new ArrayList<>();
List<String> yingwenNum = new ArrayList<>();
th.get26English(numList, yingwenNum);
new Thread(()->{
numList.forEach((o)->{
try{
Object exchange = exchanger.exchange(o);
System.out.println(exchange);
}catch (Exception e){
}
});
}).start();
new Thread(()->{
yingwenNum.forEach((o)->{
try{
Object exchange = exchanger.exchange(o);
System.out.println(exchange);
}catch (Exception e){
}
});
}).start();
}
}
此处感谢评论区大佬指出错误@感性企鹅
优化版(我就要实现)
public class Thread_two_thread_08_26 {
public static void main(String[] args) {
Exchanger<Object> exchanger = new Exchanger<>();
Thread_two_thread_07_26 th = new Thread_two_thread_07_26();
List<Integer> numList = new ArrayList<>();
List<String> yingwenNum = new ArrayList<>();
th.get26English(numList, yingwenNum);
new Thread(()->{
numList.forEach((o)->{
try{
Object exchange = exchanger.exchange(o);
Thread.sleep(1); //需要睡一下,来保证交替打印
System.out.println(exchange);
}catch (Exception e){
}
});
}).start();
new Thread(()->{
yingwenNum.forEach((o)->{
try{
Object exchange = exchanger.exchange(o);
System.out.println(exchange);
}catch (Exception e){
}
});
}).start();
}
}
1
A
2
B
3
C
4
D …