50,526
社区成员
发帖
与我相关
我的任务
分享
package app.thread;
/**
* 两个线程交替执行
*
* @author faith.huan 2019-08-13 21:25
*/
public class TwoThreadAlternateTest {
private static String nextThread = "a";
public static void main(String[] args) {
new Thread(() -> {
int n = 1;
while (n < 52) {
if (nextThread.equals("a")) {
System.out.print(n++ + "" + n++);
nextThread = "b";
}
}
}, "a").start();
new Thread(() -> {
int startChar = 65;
int times = 0;
while (times < 26) {
if (nextThread.equals("b")) {
System.out.print((char) (startChar + times++) +" ");
nextThread = "a";
}
}
}, "b").start();
}
}