[alibaba/fastjson]fastjson多线程并发反序列的时候会出现偶发的耗时过程问题

2025-11-03 743 views
4

测试代码如下,实际效果中大概会出现120次左右反序列化耗时多余120ms的情况。 不知道是不是有哪些参数可以优化下。

public class Demo { public static ParserConfig pc = new ParserConfig(); public static SerializeConfig sc = new SerializeConfig();

public static void main(String[] args) {
    for (int i=0; i<10; i++) {
        Thread thread = new Thread(new thread());
        thread.start();
    }
}

}

class thread implements Runnable {

public void run() {
    String result = "{\"5岁\":0.09540925767953422,\"10岁\":1.8547471832567268,\"35-39周岁\":0.41853773379256226,\"30-34周岁\":0.41853773379256226,\"18-24周岁\":2.3116727113957487,\"9-12岁\":8.379067065200381,\"1-3岁\":12.796200730405136,\"12岁以上\":1.8195517836730413,\"3岁\":1.854624946063406,\"3-6岁\":14.406849707305518,\"6-9岁\":8.451530863704054,\"6-12个月\":0.371032376582878,\"25-29周岁\":2.0611743120756145,\"0-1岁\":0.3341286936882935,\"9岁\":1.854095173059459}";

    ParserConfig parserConfig = new ParserConfig();

    long cost, start, end;
    for (int k=0; k<1000; k++) {
        cost=0;
        for (int j = 0; j < 40; j++) {
            start = System.currentTimeMillis();
            for (int i = 0; i < 16; i++) {
                JSON.parseObject(result, Map.class);
            }

            end = System.currentTimeMillis();

            cost += end - start;

        }

        if (cost>100) {
            System.out.println(cost);
        }
    }
}

}

回答

8

ParserConfig parserConfig = new ParserConfig(); 这个需要Singleton

8

@wenshao 没太明白你的意思,我看了下fastjson如果不提前初始化以下的确会很慢,但是也仅限于第一次,我这个问题应该不是提前初始化的问题。

4

遇到了同样的问题,老师的问题现在解决了吗?

6

遇到了同样的问题,老师的问题现在解决了吗?

ParserConfig parserConfig = new ParserConfig(); 这个需要Singleton