62,616
社区成员
发帖
与我相关
我的任务
分享
public class CopyOfTestSys1 extends Thread {
private Timer t;
public CopyOfTestSys1 (Timer t){
this.t=t;
}
public static void main(String[] args) {
Timer t=new Timer();
Thread thread1=new CopyOfTestSys1(t);
Thread thread2=new CopyOfTestSys1(t);
thread1.start();
thread2.start();
}
@Override
public void run() {
t.add(Thread.currentThread().getName());
}
}
public class Timer {
private static int number=0;
//synchronized {};
public synchronized void add(String name){
number++;
try {
Thread.sleep(1);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println( number );
}
}
就 ok了 Timer t=new Timer();
public static void main(String[] args) {
Thread thread1=new CopyOfTestSys1();
Thread thread2=new CopyOfTestSys1();
thread1.start();
thread2.start();
这不是new2个对象么。。。那timer不就是2个不同内存中的东西吗。。2个线程不共享啊。。。。public class CopyOfTestSys1 extends Thread {
Timer t=new Timer();
public static void main(String[] args) {
Thread thread1=new CopyOfTestSys1();
Thread thread2=new CopyOfTestSys1();
thread1.start();
thread2.start();
}
@Override
public void run() {
t.add(Thread.currentThread().getName());
}
}
public class Timer {
private static int number=0;
//synchronized {};
public synchronized void add(String name){
number++;
try {
Thread.sleep(1);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println( number );
}
}
更多 0