多线程同步

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();
		}
	}
	
}

你可能感兴趣的:(多线程,thread,Go)