public synchronized void setValue(int condition){ if(condition>0){ result=100; System.out.println(Thread.currentThread().getName()+" A Set Value!!!!"); } else { result=200; System.out.println(Thread.currentThread().getName()+" B Set Value!!!!"); }
System.out.println(Thread.currentThread().getName()+" The Value is "+ result); }
public static synchronized void setValue(int condition){ if(condition>0){ result=100; System.out.println(Thread.currentThread().getName()+" A Set Value!!!!"); } else { result=200; System.out.println(Thread.currentThread().getName()+" B Set Value!!!!"); }
System.out.println(Thread.currentThread().getName()+" The Value is "+ result); }
输出内容如下:
1 2 3 4
Thread-0 A Set Value!!!! Thread-0 The Value is 100 Thread-1 B Set Value!!!! Thread-1 The Value is 200