Synchronized和ReentrantLock有什么区别

直接上代码

import org.junit.Test;
import java.util.concurrent.locks.ReentrantLock;

public class ThreadSafeSample {

    public int sharedState1;
    public int sharedState2;
    public int sharedState3;

    public void nonSafeAction() {
        while (sharedState1 < 100000) {
            int former = sharedState1++;
            int latter = sharedState1;
            if (former != latter - 1) {
                System.out.println("[nonSafeAction] Observd data race,former is " + former + "" +
                        ", latter is " + latter);
            }
        }
    }

    public void safeAction() {
        while (sharedState2 < 100000) {
            //将两次赋值过程用 synchronized 保护起来,使用 this 作为互斥单元,
            // 就可以避免别的线程并发的去修改 sharedState
            synchronized (this) {
                int former = sharedState2++;
                int latter = sharedState2;
                if (former != latter - 1) {
                    System.out.println("[safeAction] Observd data race,former is " + former + "" +
                            ", latter is " + latter);
                }
            }
        }
    }

    public void safeAction2() {
        //创建公平锁
        ReentrantLock fairLock = new ReentrantLock(true);
        if(fairLock.tryLock()){
            try {
                while (sharedState3 < 100000) {
                    synchronized (this) {
                        int former = sharedState3++;
                        int latter = sharedState3;
                        if (former != latter - 1) {
                            System.out.println("[safeAction] Observd data race,former is " + former + "" +
                                    ", latter is " + latter);
                        }
                    }
                }
            } finally {
                fairLock.unlock();
            }
        }
    }


    @Test
    public void testNonSafeAction() throws  Exception{
        final ThreadSafeSample threadSafeSample = new ThreadSafeSample();
        Thread threadA = new Thread() {
            public void run() {
                threadSafeSample.nonSafeAction();
            }
        };

        Thread threadB = new Thread() {
            public void run() {
                threadSafeSample.nonSafeAction();
            }
        };

        threadA.start();
        threadB.start();
        threadA.join();
        threadB.join();
    }

    @Test
    public void testSafeAction() throws  Exception{
        final ThreadSafeSample threadSafeSample = new ThreadSafeSample();
        Thread threadA = new Thread() {
            public void run() {
                threadSafeSample.safeAction();
            }
        };

        Thread threadB = new Thread() {
            public void run() {
                threadSafeSample.safeAction();
            }
        };

        threadA.start();
        threadB.start();
        threadA.join();
        threadB.join();
    }

    @Test
    public void testSafeAction2() throws  Exception{
        final ThreadSafeSample threadSafeSample = new ThreadSafeSample();
        Thread threadA = new Thread() {
            public void run() {
                threadSafeSample.safeAction2();
            }
        };

        Thread threadB = new Thread() {
            public void run() {
                threadSafeSample.safeAction2();
            }
        };

        threadA.start();
        threadB.start();
        threadA.join();
        threadB.join();
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值