62,614
社区成员
发帖
与我相关
我的任务
分享
public class Test3 {
public static void main(String[] args) {
computer3 t = new computer3();
new Thread(t).start();
new Thread(t).start();
new Thread(t).start();
}
}
class computer3 extends Thread {
int i = 10;
static Object obj = new Object();
public void print() {
System.out.println(Thread.currentThread().getName() + ":" + i);
i--;
}
public void run() {
while (i > 0) {
synchronized (obj) {
print();
}
try {
sleep(100);
} catch (Exception e) {
}
}
}
}
while (i > 0) {
synchronized (obj) {
if(i>0){ // 加上这个if吧,后面别忘了加上}
public class Test3 {
public static void main(String[] args) {
computer3 t = new computer3();
new Thread(t).start();
new Thread(t).start();
new Thread(t).start();
}
}
class computer3 extends Thread {
int i = 10;
static Object obj = new Object();
public void print() {
System.out.println(Thread.currentThread().getName() + ":" + i);
i--;
}
public void run() {
while (i > 0) {
synchronized (this) {
print();
try {
sleep(100);
} catch (Exception e) {
}
}
}
}
}
public class Test3 {
public static void main(String[] args) {
computer3 t = new computer3();
new Thread(t).run();
new Thread(t).run();
new Thread(t).run();
}
}
class computer3 extends Thread {
int i = 10;
public synchronized void print() {
System.out.println(Thread.currentThread().getName() + ":" + i);
i--;
}
@Override
public void run() {
while(i > 0) {
print();
}
}
}