62,621
社区成员
发帖
与我相关
我的任务
分享
public class Timer {
private static int number=0;
private static Object lock = new Object();
public void add(String name){
synchronized (lock){
number++;
try {
Thread.sleep(1);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println( number );
}
}
}
public class CopyOfTestSys1 extends Thread {
private Timer t;
CopyOfTestSys1(Timer t){
this.t = t;
}
public static void main(String[] args) throws InterruptedException {
Timer tt = new Timer();
Thread thread1 = new CopyOfTestSys1(tt);
Thread thread2 = new CopyOfTestSys1(tt);
thread1.start();
thread2.start();
}
@Override
public void run() {
t.add(Thread.currentThread().getName());
}
}
public class Timer {
private static int number=0;
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 );
}
}
Thread thread1 = new CopyOfTestSys1(t);
Thread thread2 = new CopyOfTestSys1(t);public class CopyOfTestSys1 extends Thread {
private Timer t;
CopyOfTestSys1(Timer t){
this.t = t;
}
public static void main(String[] args) throws InterruptedException {
Timer tt = new Timer();
Thread thread1 = new CopyOfTestSys1(tt);
Thread thread2 = new CopyOfTestSys1(tt);
thread1.start();
thread2.start();
}
@Override
public void run() {
t.add(Thread.currentThread().getName());
}
}