java中线程同步基础

临界区:临界区是一个用以访问访问共享资源的代码块,这个代码块在同一时间内只允许一个线程执行。

重入锁:当末个线程请求一个由其他线程持有的锁时,发出的请求的线程就会阻塞,然而内置锁(synchronized)是可重入的,因此如果末个线程试图获得一个已经由他自己持有的锁,那么这个请求就会成功。

synchronized 和 ReentrantLock 都是重入锁。

public class Test1 {

    private static Object lock = new Object();
    private int i;
    
    public Test1(int i) {
        super();
        this.i = i;
    }

    public void getone()throws Exception{
        Thread.sleep(i*1000);
        synchronized (lock) {
            System.out.println("获取第二个锁");
          
            }
    }
    
    public void gettwo()throws Exception{
        Thread.sleep(i*1000);
        synchronized (lock) {
            System.out.println("获取第一个锁");
            getone();
          
        }
    }
    
    public static void main(String[] args) throws Exception{
        for(int i=1;i<4;i++){
           final Test1 test1 = new Test1(i);
            new Thread(new Runnable() {
                
                @Override
                public void run() {
                    try {
                        test1.gettwo();
                    } catch (Exception e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                }
            }).start();;
        }
    }
    
    
    
    
}

输出结果:
获取第一个锁
获取第二个锁
获取第一个锁
获取第二个锁
获取第一个锁

如果getone()方法 不能获取lock锁的对象,那么gettwo()这个方法就会一直阻塞,整个线程都会阻塞。


 

public class Test1 {

    private static ReentrantLock lock = new ReentrantLock();
    private int i;
    
    public Test1(int i) {
        super();
        this.i = i;
    }

    public void getone()throws Exception{
        Thread.sleep(i*1000);
        lock.lock();
        System.out.println("获取第二个锁");
        lock.unlock();
    }
    
    public void gettwo()throws Exception{
        Thread.sleep(i*1000);
        lock.lock();
        System.out.println("获取第一个锁");
        getone();
        lock.unlock();
    }
    
    public static void main(String[] args) throws Exception{
        for(int i=1;i<4;i++){
           final Test1 test1 = new Test1(i);
            new Thread(new Runnable() {
                
                @Override
                public void run() {
                    try {
                        test1.gettwo();
                    } catch (Exception e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                }
            }).start();;
        }
    }
    
    
    
    
}

输出结果:

获取第一个锁
获取第二个锁
获取第一个锁
获取第二个锁
获取第一个锁
获取第二个锁
lock 获取几次锁就要释放几次,不然线程就会阻塞。

lock获取锁的代码:
 

  final boolean nonfairTryAcquire(int acquires) {
            final Thread current = Thread.currentThread();
            int c = getState();
            if (c == 0) {
                if (compareAndSetState(0, acquires)) {
                    setExclusiveOwnerThread(current);
                    return true;
                }
            }
            //如果获取不到锁,进判断当前线程和持有锁的线程是否是同一个
            else if (current == getExclusiveOwnerThread()) {
                int nextc = c + acquires;
                if (nextc < 0) // overflow
                    throw new Error("Maximum lock count exceeded");
                setState(nextc);
                return true;
            }
            return false;
        }

synchronized:如果写在方法上 那么锁住的是当前对象是this,如果写在静态方法上那么锁住就是当前类的Class如:XX.Class。

ReentrantLock:  lock.tryLock() 尝试获取锁,如果获取到就true, 获取不到就返回,这样线程获取不到锁线程也不会阻塞。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值