两个线程需要按此顺序打印-
Thread1:0
Thread2:::0
Thread1:1
Thread2:::1
Thread1:2
Thread2:::2
Thread1:3
Thread2:::3
Thread1:::4
Thread2:::4
.
.
.
Thread1:100
Thread2:::100
...
Run Code Online (Sandbox Code Playgroud)
目前这是我的代码,不知何故它卡住了。. 不知道为什么它没有按预期运行
public class Solution{
private volatile boolean isOneTurn;
public Solution(){
isOneTurn = true;
}
public void test(){
Thread t1 = new Thread(new MyThread1("Thread1"));
Thread t2 = new Thread(new MyThread2("Thread2"));
t1.start();
t2.start();
}
class MyThread1 implements Runnable {
public String name;
public MyThread1(String name) {
this.name = name;
}
@SneakyThrows
@Override
public void run() {
for (int i = 1; i <= 100; …Run Code Online (Sandbox Code Playgroud)