Should GSON be announced as a static finale?

I am using Java Callable Future in my code. Below is my main code that uses the future and calling symbols -

Below is my main code that uses the future and defiant characters -

public class TimeoutThread { public static void main(String[] args) throws Exception { ExecutorService executor = Executors.newFixedThreadPool(5); Future<TestResponse> future = executor.submit(new Task()); try { System.out.println(future.get(3, TimeUnit.SECONDS)); } catch (TimeoutException e) { } executor.shutdownNow(); } } 

Below is my Task class, which implements the Callable interface, in which I am making a REST URL for my SERVERS using RestTemplate . And then I pass the response variable to the checkString method, in which I deserialize the JSON string and then check to see if it has the error or warning key in it, and then based on what make TestResponse .

 class Task implements Callable<TestResponse> { private static RestTemplate restTemplate = new RestTemplate(); @Override public TestResponse call() throws Exception { String url = "some_url"; String response = restTemplate.getForObject(url, String.class); TestResponse response = checkString(response); } } private TestResponse checkString(final String response) throws Exception { Gson gson = new Gson(); // is this an expensive call here, making objects for each and every call? TestResponse testResponse = null; JsonObject jsonObject = gson.fromJson(response, JsonObject.class); // parse, need to check whether it is an expensive call or not. if (jsonObject.has("error") || jsonObject.has("warning")) { final String error = jsonObject.get("error") != null ? jsonObject.get("error").getAsString() : jsonObject .get("warning").getAsString(); testResponse = new TestResponse(response, "NONE", "SUCCESS"); } else { testResponse = new TestResponse(response, "NONE", "SUCCESS"); } return testResponse; } 

So my question is: how do I declare a GSON here? Should it be declared as a static final global variable in my Task class? Bcoz I'm currently parsing JSON using gson, and for every call I make new Gson() , which will be expensive or not?

+8
java multithreading gson thread-safety
source share
2 answers

The Gson object Gson clearly safe to use from multiple threads, since it does not preserve any internal state, so yes, declare private static final Gson GSON = new Gson(); or even make it public .

Note that if you want your client code to be able to set up rendering using GsonBuilder , you must accept a Gson object as a parameter.

+13
source share

The Gson library can be defined at the class level and used everywhere because it does not support the state of different calls. Since it does not support state, you can declare it once and use it everywhere (one line is less than the code if you need to reuse it). Multithreading will not affect it. On a different note, although looking at the performance indicators in the official documentation, this does not seem to be a costly challenge.

0
source share

All Articles