- class Demo
- { int num = 0;
- //Object obj = new Object();
- public synchronized void show(int a)
- {
- num = num+a;
- //try{Thread.sleep(10);}catch(Exception e){}//这里不加这句话,结果也是一样的,这句话为什么要加在上面呢
- System.out.println(num);
- }
-
- }
- class Person implements Runnable
- {
- private Demo d = new Demo();
- public void run()
- {
- for(int x= 0 ;x<3;x++)
- {
- d.show(100);
- }
- }
- }
- class Demo1
- {
- public static void main(String[] args)
- {
- Person p = new Person();
- Thread t1 = new Thread(p);
- Thread t2 = new Thread(p);
- t1.start();
- t2.start();
- }
- }
复制代码
大侠 帮忙解释一下 |