王吉元-NWPU

导航

 

一、当两个并发线程访问同一个对象object中的这个synchronized(this)同步代码块时,一个时间内只能有一个线程得到执行。另一个线程必须等待当前线程执行完这个代码块以后才能执行该代码块。

package com.wjy.synchronize;

public class MyThread implements Runnable{

    @Override
    public void run() {
        // TODO Auto-generated method stub
        synchronized (this) {
            for(int i=0;i<10;i++){
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                System.out.println(Thread.currentThread().getName()+"   loop   "+i);
            }
        }
    }
    
}

测试代码:

package com.wjy.test;

import com.wjy.synchronize.MyThread;

public class MainTestSyn {
    public static void main(String args[]){
        MyThread myThread=new MyThread();
        
        Thread thread1=new Thread(myThread, "buDog");
        Thread thread2=new Thread(myThread, "paoDog");
        
        thread1.start();
        thread2.start();
    }
}

运行结果:

buDog   loop   0
buDog   loop   1
buDog   loop   2
buDog   loop   3
buDog   loop   4
buDog   loop   5
buDog   loop   6
buDog   loop   7
buDog   loop   8
buDog   loop   9
paoDog   loop   0
paoDog   loop   1
paoDog   loop   2
paoDog   loop   3
paoDog   loop   4
paoDog   loop   5
paoDog   loop   6
paoDog   loop   7
paoDog   loop   8
paoDog   loop   9

//*********************************************************************************************************************

 二、然而,当一个线程访问object的一个synchronized(this)同步代码块时,另一个线程仍然可以访问该object中的非synchronized(this)同步代码块。

三、尤其关键的是,当一个线程访问object的一个synchronized(this)同步代码块时,其他线程对object中所有其它synchronized(this)同步代码块的访问将被阻塞。

 

拿二和三同时比较:

先看看二的代码:

package com.wjy.synchronize;

public class MyThread implements Runnable{

    
    public void fun1(){
        synchronized (this) {
            for(int i=0;i<10;i++){
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                System.out.println(Thread.currentThread().getName()+"   loop fun1   "+i);
            }
        }
    }
    
    public void fun2(){
        //synchronized (this) {
            for(int i=0;i<10;i++){
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                System.out.println(Thread.currentThread().getName()+"   loop fun2   "+i);
            }
        //}
    }
    @Override
    public void run() {
        // TODO Auto-generated method stub
        
    }
    
}

测试代码:

package com.wjy.test;

import com.wjy.synchronize.MyThread;

public class MainTestSyn {
    public static void main(String args[]){
        final MyThread myThread=new MyThread();
        
//        Thread thread1=new Thread(myThread, "buDog");
//        Thread thread2=new Thread(myThread, "paoDog");
        
        Thread thread1=new Thread(new Runnable() {
            
            @Override
            public void run() {
                // TODO Auto-generated method stub
                myThread.fun1();
            }
        }, "buDog");
        Thread thread2=new Thread(new Runnable() {
            
            @Override
            public void run() {
                // TODO Auto-generated method stub
                myThread.fun2();
            }
        }, "paoDog");
        thread1.start();
        thread2.start();
    }
}

结果:

buDog   loop fun1   0
paoDog   loop fun2   0
buDog   loop fun1   1
paoDog   loop fun2   1
buDog   loop fun1   2
paoDog   loop fun2   2
paoDog   loop fun2   3
buDog   loop fun1   3
paoDog   loop fun2   4
buDog   loop fun1   4
paoDog   loop fun2   5
buDog   loop fun1   5
paoDog   loop fun2   6
buDog   loop fun1   6
paoDog   loop fun2   7
buDog   loop fun1   7
paoDog   loop fun2   8
buDog   loop fun1   8
buDog   loop fun1   9
paoDog   loop fun2   9

但是来看看三的代码,三的代码很简单就是在二的fun2()中的代码置为

synchronized 即可,就是将二中fun2()的注释释放就行了,其余都不用动。看看运行结果:
buDog   loop fun1   0
buDog   loop fun1   1
buDog   loop fun1   2
buDog   loop fun1   3
buDog   loop fun1   4
buDog   loop fun1   5
buDog   loop fun1   6
buDog   loop fun1   7
buDog   loop fun1   8
buDog   loop fun1   9
paoDog   loop fun2   0
paoDog   loop fun2   1
paoDog   loop fun2   2
paoDog   loop fun2   3
paoDog   loop fun2   4
paoDog   loop fun2   5
paoDog   loop fun2   6
paoDog   loop fun2   7
paoDog   loop fun2   8
paoDog   loop fun2   9

这里必须声明:以上所有同步块修改为同步方法结果不变,就是在方法上声明
synchronized ,像这样:public synchronized void fun1(){}
posted on 2013-12-30 16:13  王吉元  阅读(280)  评论(0编辑  收藏  举报