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


    在JVM中具有String常量池缓存的功能

    package com.cky.test;
    
    /**
     * Created by edison on 2017/12/8.
     */
    public class Test {
        public static void main(String[] args) {
           String a= "a";
           String b= "a";
            System.out.println(a==b);
    
        }
    }
    true

    将synchronized(string)同步代码块和String联合使用,结果出现意外

    测试

    package com.cky.bean;
    
    /**
     * Created by edison on 2017/12/8.
     */
    public class Service {
    
        public static  void print(String str) {
            try {
                synchronized (str) {
                    while(true) {
                        System.out.println(Thread.currentThread().getName());
                        Thread.sleep(1000);
                    }
                }
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
    
        }
    
    
    
    }
    package com.cky.thread;
    
    import com.cky.bean.Service;
    
    /**
     * Created by edison on 2017/12/8.
     */
    public class ThreadA extends  Thread{
        private Service service;
    
        public ThreadA(Service service) {
            this.service = service;
        }
    
        @Override
        public void run() {
            super.run();
            service.print("AA");
        }
    }
    package com.cky.thread;
    
    import com.cky.bean.Service;
    
    /**
     * Created by edison on 2017/12/8.
     */
    public class ThreadB extends  Thread{
    
    
        private Service service;
    
        public ThreadB(Service service) {
            this.service = service;
        }
    
        @Override
        public void run() {
            super.run();
            service.print("AA");
        }
    }
    package com.cky.test;
    
    import com.cky.bean.Service;
    import com.cky.thread.ThreadA;
    import com.cky.thread.ThreadB;
    
    /**
     * Created by edison on 2017/12/8.
     */
    public class Test {
        public static void main(String[] args) {
            Service service = new Service();
            ThreadA threadA = new ThreadA(service);
            threadA.setName("a");
            threadA.start();
            ThreadB threadB = new ThreadB(service);
            threadB.setName("b");
            threadB.start();
    
        }
    }
    a
    a
    a
    a
    a
    a
    a
    a

    结果是无限循环的打印a

    出现这种情况的原因是因为String的两个值都是AA,两个线程持有相同的锁。所以造成线程B不能执行,这就是String常量2池带来的问题,所以大多数情况下都不使用String做所对象,比如new Object实例化一个Object对象,但他不放入缓存中

    测试

    package com.cky.bean;
    
    /**
     * Created by edison on 2017/12/8.
     */
    public class Service {
    
        public static  void print(Object object) {
            try {
                synchronized (object) {
                    while(true) {
                        System.out.println(Thread.currentThread().getName());
                        Thread.sleep(1000);
                    }
                }
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
    
        }
    
    
    
    }
    package com.cky.thread;
    
    import com.cky.bean.Service;
    
    /**
     * Created by edison on 2017/12/8.
     */
    public class ThreadA extends  Thread{
        private Service service;
    
        public ThreadA(Service service) {
            this.service = service;
        }
    
        @Override
        public void run() {
            super.run();
            service.print(new Object());
        }
    }
    package com.cky.thread;
    
    import com.cky.bean.Service;
    
    /**
     * Created by edison on 2017/12/8.
     */
    public class ThreadB extends  Thread{
    
    
        private Service service;
    
        public ThreadB(Service service) {
            this.service = service;
        }
    
        @Override
        public void run() {
            super.run();
            service.print(new Object());
        }
    }
    package com.cky.test;
    
    import com.cky.bean.Service;
    import com.cky.thread.ThreadA;
    import com.cky.thread.ThreadB;
    
    /**
     * Created by edison on 2017/12/8.
     */
    public class Test {
        public static void main(String[] args) {
            Service service = new Service();
            ThreadA threadA = new ThreadA(service);
            threadA.setName("a");
            threadA.start();
            ThreadB threadB = new ThreadB(service);
            threadB.setName("b");
            threadB.start();
    
        }
    }
    a
    b
    b
    a
    a
    b
    a
    b
    b
    a

    交替打印说明一个一个对象锁

  • 相关阅读:
    图片预加载的JS代码
    JavaScript实现漫天飞花及文字滚动特效的代码
    判断远程图片是否存在的JavaScript代码
    dos批处理命令详解
    内存虚拟盘软件XMSDSK的使用
    医学论坛(收集)
    velocity
    java开源项目 源代码
    开放源代码的全文检索引擎Lucene
    批处理命令大全
  • 原文地址:https://www.cnblogs.com/edison20161121/p/8001084.html
Copyright © 2020-2023  润新知