一 .什么是String的常量池特性

对于字符串对象有两种创建方法,如下:

直接赋值法:

String str1="直接赋值创建字符串";

创建对象法:

String str2=new String("创建对象来创建字符串");

第一种方法是直接创建在常量池中的,下面可以看到区别:

/**
* @ClassName String_Synchronized
* @Author 真正的小明被占用了
* @Date 2018/10/21/021 14:54
* @Version 1.0
*/
public class String_Synchronized { public static void main(String[] args) {
String str1="A";
String str2="A";
System.out.println(str1==str2);// true 比较两个变量的地址值,输出为TRUE证明两个对象为同一个对象
String str3=new String("B");
String str4=new String("B");
System.out.println(str3==str4);//false
}
}

也就是说明在常量池中创建的对象是同一个对象,而使用new关键字创建的变量是重新分配内存的,是两个不同的对象。

二.在多线程中出现的问题

代码如下:

将一个类的String对象同步化

/**
* @ClassName String_Synchronized
* @Author 真正的小明被占用了
* @Date 2018/10/21/021 14:54
* @Version 1.0
*/
public class String_Synchronized { private String str; public String_Synchronized(String str){
this.str=str;
} public void fan() throws Exception {
synchronized (str){//同步化String对像
//假如这个方法需要大量的时间去执行多线程可以提高效率.....我们使用死循环来代替
while(true){
Thread.sleep(1000);
System.out.println("当前的线程为"+Thread.currentThread().getName());
}
}
}
}

线程类:

public class Str_Thread extends Thread{
private String_Synchronized string_synchronized; public Str_Thread(String_Synchronized string_synchronized){
this.string_synchronized=string_synchronized;
} @Override
public void run(){
try {
string_synchronized.fan();
} catch (Exception e) {
e.printStackTrace();
}
} }

测试类:

public class Text {

    public static void main(String[] args) {
String str1 ="A";
String str2 ="A";
String_Synchronized string_synchronized1 =new String_Synchronized(str1);
String_Synchronized string_synchronized2 =new String_Synchronized(str2);
Str_Thread str_thread1 =new Str_Thread(string_synchronized1);
Str_Thread str_thread2 =new Str_Thread(string_synchronized2);
str_thread1.start();
str_thread2.start();
} }

结果就是:说明str_thread1线程没有没打断,出现死锁现象。

我们修改一下代码:

String str1 =new String ("A");
String str2 =new String ("A");

结果为:,现在即时两个线程叫错打印。

三.线程死锁的原因

造成线程死锁的本质原因就是:双方互相持有对方的锁,互相等待对方释放锁那么一定会造成死锁。

String对象常量池特性对synchronized对象的影响的更多相关文章

  1. java 多线程10:synchronized锁机制 之 锁定类静态方法 和锁定类.Class 和 数据String的常量池特性

    同步静态方法 synchronized还可以应用在静态方法上,如果这么写,则代表的是对当前.java文件对应的Class类加锁.看一下例子,注意一下printC()并不是一个静态方法: public ...

  2. 2.2.10数据类型String的常量池特性

    在JVM中具有String常量池缓存的功能 package com.cky.test; /** * Created by edison on 2017/12/8. */ public class Te ...

  3. Java中的String与常量池[转帖]

    string是java中的字符串.String类是不可变的,对String类的任何改变,都是返回一个新的String类对象.下面介绍java中的String与常量池. 1. 首先String不属于8种 ...

  4. Java中的String与常量池

    string是java中的字符串.String类是不可变的,对String类的任何改变,都是返回一个新的String类对象.下面介绍java中的String与常量池. 1. 首先String不属于8种 ...

  5. 转载:Java中的String与常量池

    转载自http://developer.51cto.com/art/201106/266454.htm.感觉总结的不错,自己收藏一下. string是java中的字符串.String类是不可变的,对S ...

  6. (转)Java中的String与常量池

    Java中的String与常量池 转自:http://developer.51cto.com/art/201106/266454.htm string是java中的字符串.String类是不可变的,对 ...

  7. 基本数据类型的常量池与String类型常量池解析

    抛出样例: Integer a1  = new Integer(123);        Integer a2  = new Integer(123);        System.out.print ...

  8. String之常量池小结

    1.String 常量池 String使用private final char value[ ]实现字符串的存储,也就是说String创建对象之后不能够再次修改此对象中存储的字符串内容,因而Strin ...

  9. String对象常量池

    对象池的主要目的是实现数据的共享处理, 在java之中对象池可以分为两种: 1.静态常量池 :指*.class加载时会自动将此程序之中保存的 字符串.普通的常量.类和方法的信息等全部经行分配 2.运行 ...

随机推荐

  1. 【转】在Express项目中使用Handlebars模板引擎

    原文:http://fraserxu.me/2013/09/12/Using-Handlebarsjs-with-Expressjs/ 最近在用Expressjs做一个项目,前后端都用它来完成.自己之 ...

  2. EasyUI的textbox的disable ,readonly 用法

    EasyUI的textbox,如果用了disable, 那么提交时,后台mvc controller是取不到值的, 如果用了Readonly, textbox的样式又没有变化, 让人一眼就感知到哪些是 ...

  3. 关于scrapy

    Scrapy安装 1,Pip install wheel 2,pip install 复制路径+文件名Twisted-18.7.0-cp36-cp36m-win_amd64.whl 3,Pip ins ...

  4. 8款非常不错的.Net反编译利器

    本人搜集了下8款非常不错的.Net反编译利器: 1.Reflector Reflector是最为流行的.Net反编译工具.Reflector是由微软员工Lutz Roeder编写的免费程序.Refle ...

  5. js的关于for的语句

    JavaScript for...in 语句 for...in 语句用于对数组或者对象的属性进行循环操作. for ... in 循环中的代码每执行一次,就会对数组的元素或者对象的属性进行一次操作. ...

  6. ubuntu16.04安装anaconda、环境配置

    anaconda默认3.7降级到3.6 conda install python=3.6 anaconda安装后找不到conda命令: 执行测试命令 conda info -e conda: comm ...

  7. hibernate @OneToMany等注解设置查询过滤条件

    如实体PdOrg对象中有users对象,数据库user表有字段DEL_FLAG(0:删除:1:未删除): private List<User> users= new ArrayList&l ...

  8. EFCore Owned Entity Types,彩蛋乎?鸡肋乎?之鸡肋篇

    鸡肋 鸡肋(Chicken ribs),现代汉语词语,出自<三国志·魏书·武帝纪>裴松之注引<九州春秋>曰:"夫鸡肋,弃之如可惜,食之无所得,以比汉中,知王欲还也.& ...

  9. canvas初体验

    利用画布,绘制随机大小,颜色,位置 方框<!DOCTYPE html> <html lang="en"> <head> <meta cha ...

  10. poj1002总结

    1.之前一直是runtime error,没有找出具体原因,把冒牌排序改成了合并排序,当输入是100000行时,从大约10s变到1s内 2.感觉是atoi指针导致的,当判断atoi的入参不为NULL时 ...