100分问一个线程同步问题,在线等.谢谢!
麻烦大家帮我看看,我已经在add上用了同步,为什么还是没效果,输出1010?
谢谢大家!
class A
{
public static int date=1000;//共享资源
}
class myThread extends Thread
{
public int mydate;
public int sleeptime;
public myThread(int sleeptime,String name)
{
super(name);
this.sleeptime=sleeptime;
}
public void run()
{
System.out.println(this.getName()+ "启动 ");
add();
System.out.println(this.getName()+ "结束 ");
}
public synchronized void add()
{
try
{
mydate=A.date;
System.out.println(this.getName()+ "的A.date= "+mydate);
Thread.sleep(1000*sleeptime); //睡眠sleeptime秒
mydate=mydate+10;
A.date=mydate;
}
catch(InterruptedException ie){ }