- class Synchronized implements Runnable
- {
- public int i=10;
- Object obj=new Object();
- public void fun(){
- for(;i>0;i--)
- try{Thread.sleep(10);}catch(Exception e){}
- System.out.println(Thread.currentThread().getName()+" "+i);
- }
- public void run(){
- fun();
- }
- };
- class Main1204
- {
- public static void main(String[] args)
- {
- Synchronized s=new Synchronized();
- Thread t1=new Thread(s);
- Thread t2=new Thread(s);
- Thread t3=new Thread(s);
- t1.start();
- t2.start();
- t3.start();
- }
- }
复制代码
要崩溃了,谁能解释一下这个代码的答案为什么是这个?加锁用法越来越迷。。。
|
|