Java多线程中的锁

 

Java多线程的锁主要有4种情况,

 

1. 静态方法中的锁,此时,虚拟机使用的锁为该方法所在类的Class,即用来创建所有该类实例的那个模版,是全局唯一的,无论何种情况下调用该方法,均是线程安全的:

 

public class Test {
    public static void main(String[] args) {
        for(int i = 0;i<5;i++){
            new R().start();
        }
    }
}

class R extends Thread{
    static int i=0;
    static Set set = new HashSet();

    public void run(){
        while(i<1000000)
            multiThread();
    }

    public static synchronized void multiThread(){
        if(!set.add(i++))
            System.out.println(i+" is already exists");
    }
}

 

2. 非静态方法中的锁,此时虚拟机使用这个类的实例来作为锁,如果创建了多个实例,各个实例再同时调用该方法,那么这个方法便不安全了:

 

public class Test {
    public static void main(String[] args) {

        for(int i = 0;i<5;i++){
            new R().start();
        }
    }
}

class R extends Thread{
    static int i=0;
    static Set set = new HashSet();

    public void run(){
        while(i<1000000)
            multiThread();
    }

    public synchronized void multiThread(){
        if(!set.add(i++))
            System.out.println(i+" is already exists");
    }
}

 这时候会打印一大堆信息出来,如果这个类只创建了一个实例,别的类来调用这个实例的synchronized方法,便是线程安全的:

public class Test {
    public static void main(String[] args) {
        for(int i = 0;i<5;i++){
            new T().start();
        }
    }
}

class R{
    static Set set = new HashSet();
    static Integer i=0;

    public synchronized void multiThread(){
        if(!set.add(i++))
            System.out.println(i+" is already exists");
    }
}

class T extends Thread{
    static R r = new R();

    public void run(){
        int i = 0;
        while(i++<100000)
            r.multiThread();
    }
}

 

  3.  代码片段中加上synchronized,使用本实例做锁:

 

public class Test {
    public static void main(String[] args) {
        for(int i = 0;i<5;i++){
            new R().start();
        }
    }
}

class R extends Thread{
    static int i=0;
    static Set set = new HashSet();
    public void run(){
        while(i<1000000)
            multiThread();
    }

    public void multiThread(){
        synchronized(this){
            if(!set.add(i++))
            System.out.println(i+" is already exists");
        }        
    }
}

 这种情况和2一样,只有在这个实例仅存在一个的时候,代码段才是线程安全的,上面的代码中创建了5个R的实例,所以看起来线程安全的代码并不安全,此时,应该加上一个static的对象来作为线程锁,即:

 

4. 代码片段中加上synchronized,创建一个static的对象做锁:

 

public class Test {
    public static void main(String[] args) {
        for(int i = 0;i<5;i++){
            new R().start();
        }
    }
}

class R extends Thread{
    static int i=0;
    static Set set = new HashSet();
    static Object lock = new Object();
    public void run(){
        while(i<1000000)
            multiThread();
    }

    public void multiThread(){
        synchronized(lock){
            if(!set.add(i++))
            System.out.println(i+" is already exists");
        }        
    }
}

 此时,该方法是绝对线程安全的。

 

一般来说,采用方法的synchronized比采用代码块的synchronized执行效率要高,从他们生成的虚拟机执行码可以分析出来(没证实过,有兴趣的可以用javap去反编译一下class文件比较一下)

JDK1.5以后,java.util.concurrent这个包里面有大量的多线程环境下可以使用的类,有能够用上的就最好用这个包里面的类,不要再自己去写同步代码了,例如典型的售票程序,可以使用AtomicInteger类来处理:

public class Test {
    public static void main(String[] args) {
        for(int i = 0;i<5;i++){
            new R().start();
        }
    }
}

class R extends Thread{
    static AtomicInteger ticket = new AtomicInteger();
    static Set set = new HashSet();    
    public void run(){
        while(ticket.get() <1000000 )
            multiThread();
    }

    public void multiThread(){
        if(!set.add(ticket.addAndGet(1)))
            System.out.println(ticket.get()+" is already exists");   
    }
}

 这样子看起来是不是感觉要清爽一点了?

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值