62,614
社区成员
发帖
与我相关
我的任务
分享
public class DD {
private static final DD dd=new DD();
public static void main(String[] args) {
Mythread t1 = new Mythread("hello");
Mythread t2 = new Mythread("world");
t1.start();
t2.start();
}
static class Mythread extends Thread {
private String data;
public Mythread(String data) {
this.data = data;
}
public void run() {
synchronized (dd) {
for (int i = 0; i < 10; i++) {
try {
Thread.sleep((int) (Math.random() * 100));
} catch (Exception e) {
// TODO: handle exception
}
System.out.println(data);
}
}
}
}
}
Mythread t1 = new Mythread("hello");
Thread tt=new Thread(t1,"hello2");
Thread t2=new Thread(t1,"world");
类似这样才对