Java static and streaming security or what to do

I am expanding the library to do some work for me. Here is the code:

public static synchronized  String decompile(String source, int flags,UintMap properties,Map<String,String> namesMap)
    {
        Decompiler.namesMap=namesMap;
        String decompiled=decompile(source,flags,properties);
        Decompiler.namesMap=null;
        return decompiled;

    }

The problem is that it namesMapis a static variable. Is this thread safe or not? Because if this code is executed with confidence, the namesMap variable may change. What can I do for this?

+3
source share
4 answers

decompile ( ), - , , namesMap, , : , decompile, , , decompile, .: -)

java.util.concurrent (, ConcurrentHashMap), , - , .

( .) namesMap decompile, - ( ..), . , , , , .

+5

, namesMap, decompile(String, int, UintMap, Map), , , .

Decompiler.namesMap= new HashMap<String, String>(namesMap);

, , , , , decompile() namesMap .

+1

In fact, Decompiler.namesMap=namesMap; this is the only place to install namesMap

But nowhere in the code does changeMap change. Just read ...

I want to

String decompiled=decompile(source,flags,properties);

will use the same Map names.

0
source

There will be only one name map, so you don’t have to worry if it will use the same Map names. It would be.

0
source

Source: https://habr.com/ru/post/1741965/


All Articles