使用String.intern() 减小synchronized锁字粒度

String的intern()方法,其实总结一句就是调用这个方法之后把字符串对象加入常量池中,常量池我们都知道他是存在于方法区的,他是方法区的一部分,而方法区是线程共享的,所以常量池也就是线程共享的,但是他并不是线程不安全的,他其实是线程安全的,他仅仅是让有相同值的引用指向同一个位置而已,如果引用值变化了,但是常量池中没有新的值,那么就会新开辟一个常量结果来交给新的引用,而并非像线程不同步那样,针对同一个对象,new出来的字符串和直接赋值给变量的字符串存放的位置是不一样的,前者是在堆里面,而后者在常量池里面,另外,在做字符串拼接操作,也就是字符串相"+"的时候,得出的结果是存在在常量池或者堆里面,这个是根据情况不同不一定的,我写了几行代码测试了一下。

  先上结果:

    1.直接定义字符串变量的时候赋值,如果表达式右边只有字符串常量,那么就是把变量存放在常量池里面。

    2.new出来的字符串是存放在堆里面。

    3.对字符串进行拼接操作,也就是做"+"运算的时候,分2中情况:

      i.表达式右边是纯字符串常量,那么存放在栈里面。

      ii.表达式右边如果存在字符串引用,也就是字符串对象的句柄,那么就存放在堆里面。

package com.syn;

/**
 * Created by Liuxd on 2018-08-10.
 */
public class TestIntern {

    public static void main(String[] args) {
        String str1 = "aaa";
        String str2 = "bbb";
        String str3 = "aaabbb";
        String str4 = str1 + str2;
        String str5 = "aaa" + "bbb";
        System.out.println(str3 == str4); // false
        System.out.println(str3 == str4.intern()); // true
        System.out.println(str3 == str5);// true
    }
}
false
true
true

  结果:str1、str2、str3、str5都是存在于常量池,str4由于表达式右半边有引用类型,所以str4存在于堆内存,而str5表达式右边没有引用类型,是纯字符串常量,就存放在了常量池里面。其实Integer这种包装类型的-128 ~ +127也是存放在常量池里面,比如Integer i1 = 10;Integer i2 = 10; i1 == i2结果是true,估计也是为了性能优化。

 

直接贴代码:

1、主方法类

package com.syn;

/**
 * Created by Liuxd on 2018-08-10.
 */
public class TestSyn extends Thread {

    private TestPrint testPrint;
    private String str;

    public TestSyn(TestPrint testPrint, String str) {
        this.testPrint = testPrint;
        this.str = str;
    }

    public void run() {
        try {
            System.out.println(Thread.currentThread().getId() + "开始执行...");
            testPrint.print(str);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {

        TestPrint testPrint = new TestPrint();
        for (int i = 1; i < 100; i++) {
//          String str = "锁定订单ID"+i;
            String str = "锁定订单ID";//替换本行与上一行,执行时间会有区别
            TestSyn testSyn = new TestSyn(testPrint, String.valueOf(str));
            testSyn.start();
        }


    }


}

2、操作类

package com.syn;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * Created by Liuxd on 2018-08-10.
 */
public class TestPrint {
    public static SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//格式化

    public void print(String str) {
        synchronized (str.intern()) {
            try {
                Thread.sleep(1000);

                System.out.println(Thread.currentThread().getId() + "打印操作:" + str + "  时间" + sdf.format(new Date()));
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

}

参考:

https://www.cnblogs.com/dreamroute/p/3650399.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

春风化作秋雨

你的鼓励将是我创作的最大动力!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值