package multithreading;
/**
* in the practical problem
* the more synchronized means you make waiting time longer
* so,we should make synchronized shorter.
* @author Administrator
*
*/
public class SynTest implements Runnable {
private static int a=1;
public static void main(String[] args)
{
Thread t1=new Thread(new SynTest());
Thread t2=new Thread(new SynTest());
t1.start();
t2.start();
}
public static void go()
{
a+=1;
Thread.yield();
a-=1;
System.out.println(a);
}
@Override
public void run() {
for(int i=0;i<100;i++)
synchronized (this){
go();
}
}
}
多线程同步
最新推荐文章于 2023-07-11 22:44:47 发布