简单线程同步问题,为什么synchronized没起作用?求助

小凤凤是我 2014-08-22 03:16:53
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 );

}


}
...全文
1602 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
鱿鱼x 2014-08-25
  • 打赏
  • 举报
回复
给你理一理思路,你知道 new CopyOfTestSys1(); 发生什么事情吗? 两次new CopyOfTestSys1(); 各自new 一个Timer实例,两个Timer 实例对应一个静态的number; //以下回答你所问 synchronized 加在成员方法上,这个锁属于各自的Timer实例,你要访问两个不同的资源,当然不会同步啦。 解决: 把add 加static,但是run的方法要跟着改,不知道跟你的原意是否违背 楼上的标准方案
乔不思 2014-08-23
  • 打赏
  • 举报
回复
你这个 根本操作就是 两个 对象,何来 同步。。。。

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了
sg19911227 2014-08-22
  • 打赏
  • 举报
回复
是两个内存中的东西 但是你调用的是同一个方法啊 加安全机制是保证两个线程访问该方法时有一个顺序
tyilack_小小黑 2014-08-22
  • 打赏
  • 举报
回复
用Timer类去实现Runnable接口吧,让后再只创建一个县城对象,里面传出2个Timer对象
chengxu2011 2014-08-22
  • 打赏
  • 举报
回复
其实我想说的是 这为什么会有线程安全的问题
  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个线程不共享啊。。。。
码上行动_Light 2014-08-22
  • 打赏
  • 举报
回复
你为什么提两个一样的问题?分多用不了?
小凤凤是我 2014-08-22
  • 打赏
  • 举报
回复
你改了什么?
业余草 2014-08-22
  • 打赏
  • 举报
回复
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

62,616

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧