线程同步 你的synchronized是加在this,也就是当前对象上的。而你两个线程中创建了两个不同的Timer对象。同步当然没有效果了。 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 你的写法创建了两个Timer对象,怎么可能同步?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()); }} 改成这样:Thread thread1 = new CopyOfTestSys1(t); Thread thread2 = new CopyOfTestSys1(t); 同上面各位同学:两个Timer是无法实现同步的。改为如下: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 ); } } 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 ); } } } .jar 文件的打开问题 如何获取JFrame标题栏宽度 在Eclipse中使用Ant的时候,怎么设置系统环境变量 javaw方式启动程序怎样传递JVM参数 试过解决JDBC连接oracl遇到问题. java 应用程序只运行一次的问题 Servlet、JSP、J2EE怎样才能学到手 大神看看我写的有什么不妥吗? 有谁知道错在哪里啊??? 各位大虾,救命啊!有关jdbc的问题。100分送上! StringBuffer问题 只有编译时,才能使用字符串常量池吗?心中的几个疑问。
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());
}}
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());
}
}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 ); }
}
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 );
}
}
}