公平锁和非公平锁

转载于《Java多线程核心技术》

定义:
公平锁:按照线程加锁的顺序来分配,即先来先得FIFO;
非公平锁:一种获取锁的抢占机制,是随机的获得锁的,这样可能会有些线程一直会拿不到锁,结果也就是不公平的。

1.公平锁实例代码:

package test.Thread4;


import java.util.concurrent.locks.ReentrantLock;

class Service{
    private ReentrantLock lock;

    public Service(boolean isFair) {
        lock = new ReentrantLock(isFair);//true为公平锁
    }

    public void serviceMethod(){
        try{
            lock.lock();
            System.out.println(Thread.currentThread().getName()+"获得锁");
        }finally {
            lock.unlock();
        }
    }
}
public class FaieLock {
    public static void main(String[] args) {
        final Service service = new Service(true);
        Runnable runnable = new Runnable() {
            public void run() {
                System.out.println(Thread.currentThread().getName()+"运行了");
                service.serviceMethod();
            }
        };
        Thread[] threads = new Thread[10];
        for(int i=0;i<10;i++){
            threads[i] = new Thread(runnable);
        }
        for (int i = 0; i <10 ; i++) {
            threads[i].start();
        }
    }
}

输出结果:

Thread-0运行了
Thread-1运行了
Thread-2运行了
Thread-1获得锁
Thread-2获得锁
Thread-0获得锁
Thread-3运行了
Thread-3获得锁
Thread-4运行了
Thread-4获得锁
Thread-5运行了
Thread-5获得锁
Thread-6运行了
Thread-6获得锁
Thread-8运行了
Thread-8获得锁
Thread-7运行了
Thread-7获得锁
Thread-9运行了
Thread-9获得锁


2.非公平锁示例代码:

package test.Thread4;


import java.util.concurrent.locks.ReentrantLock;

class Service{
    private ReentrantLock lock;

    public Service(boolean isFair) {
        lock = new ReentrantLock(isFair);//true为公平锁
    }

    public void serviceMethod(){
        try{
            lock.lock();
            System.out.println(Thread.currentThread().getName()+"获得锁");
        }finally {
            System.out.println(Thread.currentThread().getName()+"释放了锁");
            lock.unlock();
        }
    }
}
public class FaieLock {
    public static void main(String[] args) {
        final Service service = new Service(false);//非公平锁
        Runnable runnable = new Runnable() {
            public void run() {
                System.out.println(Thread.currentThread().getName()+"运行了");
                service.serviceMethod();
            }
        };
        Thread[] threads = new Thread[10];
        for(int i=0;i<10;i++){
            threads[i] = new Thread(runnable);
        }
        for (int i = 0; i <10 ; i++) {
            threads[i].start();
        }
    }
}

运行结果可能为

Thread-0运行了
Thread-0获得锁
Thread-1运行了
Thread-0释放了锁
Thread-2运行了
Thread-2获得锁
Thread-2释放了锁
Thread-1获得锁
Thread-1释放了锁
Thread-3运行了
Thread-3获得锁
Thread-3释放了锁
Thread-5运行了
Thread-5获得锁
Thread-5释放了锁
Thread-6运行了
Thread-6获得锁
Thread-6释放了锁
Thread-7运行了
Thread-7获得锁
Thread-7释放了锁
Thread-8运行了
Thread-8获得锁
Thread-8释放了锁
Thread-9运行了
Thread-9获得锁
Thread-9释放了锁
Thread-4运行了
Thread-4获得锁
Thread-4释放了锁

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值