1.4K Star 5K Fork 1.7K

GVP红薯 / J2Cache

 / 详情

kryo使用方式会有线程安全问题

已完成
创建于  
2015-03-17 12:18

kryo非线程安全的,并发的情况下使用kryo会有问题,测试代码如下:


static class Persion {
        private String name ;
        private int age ;

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public int getAge() {
            return age;
        }

        public void setAge(int age) {
            this.age = age;
        }

        public Persion(String name, int age) {
            this.name = name;
            this.age = age;
        }
    }

    public static void main(String[] args) {

        ExecutorService executorService = Executors.newFixedThreadPool(2);
        executorService.execute(new Runnable() {
            @Override
            public void run() {
                while (true){
                    Persion persion = new Persion("wens", 29);
                    try {
                        SerializationUtils.serialize(persion) ;
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }

            }
        });
        executorService.execute(new Runnable() {
            @Override
            public void run() {
                while (true){
                    Persion persion = new Persion("wens", 29);
                    try {
                        SerializationUtils.serialize(persion) ;
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }

            }
        });

        executorService.shutdown();
    }

评论 (6)

Kryo is not thread safe. Each thread should have its own Kryo instance

SerializationUtils
10000000 次
耗时 实现方式

  • 13927 fst2.25
  • 18357 fst1.63
  • 36401 jdk1.7
  • 28627 kryo 3.0
  • 9329 kryo_pool_ser 3.0

直接升级JDK1.7用fst2.25

@杨洪河 @温佐镜 KryoPoolSerializer 已经处理 线程安全问题,请使用kryo_pool_ser 方案。

这个issue还不关闭吗?现在推荐使用kryo是吧?

kryo 我们自己没有在生产环境中使用过,所以不敢推荐 @yangghosts

状态更改为 已关闭

登录 后才可以发表评论

状态
负责人
里程碑
Pull Requests
关联的 Pull Requests 被合并后可能会关闭此 issue
分支
开始日期   -   截止日期
-
置顶选项
优先级
参与者(4)
7769 yangghosts 1578914513 1052 502959937 1578913935 36 ld 1578913711
Java
1
https://gitee.com/ld/J2Cache.git
git@gitee.com:ld/J2Cache.git
ld
J2Cache
J2Cache

搜索帮助