Will creating new objects help prevent concurrency issues inside a static method?
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}
I have this method that accepts an OkHttp#response
:
public static Map<String, Object> getResponseBody(Response response) throws IOException {
return new ObjectMapper()
.readValue(response.body().string(),
new TypeReference<Map<String, Object>>() {
});
}
From what I understand is that if multiple classes use the getResponseBody
, I'll encounter big problems as they'll all be accessing the same Response
.
Will it be solved if I do this?:
public static Map<String, Object> getResponseBody(Response response) throws IOException {
ResponseBody responseBody = response.body();
String responseString = responseBody.string();
ObjectMapper mapper = new ObjectMapper();
Map<String, Object> map = mapper
.readValue(responseString,
new TypeReference<Map<String, Object>>() {
});
return map;
}
java concurrency static
|
show 2 more comments
I have this method that accepts an OkHttp#response
:
public static Map<String, Object> getResponseBody(Response response) throws IOException {
return new ObjectMapper()
.readValue(response.body().string(),
new TypeReference<Map<String, Object>>() {
});
}
From what I understand is that if multiple classes use the getResponseBody
, I'll encounter big problems as they'll all be accessing the same Response
.
Will it be solved if I do this?:
public static Map<String, Object> getResponseBody(Response response) throws IOException {
ResponseBody responseBody = response.body();
String responseString = responseBody.string();
ObjectMapper mapper = new ObjectMapper();
Map<String, Object> map = mapper
.readValue(responseString,
new TypeReference<Map<String, Object>>() {
});
return map;
}
java concurrency static
From what I understand is that if multiple classes use the getResponseBody, I'll encounter big problems as they'll all be accessing the same Response. That is not correct.
– Elliott Frisch
Nov 27 '18 at 0:01
Oh alright. May I ask for your correction?
– Rigo Sarmiento
Nov 27 '18 at 0:01
Every thread that calls thegetResponseBody
method will provide a thread localResponse
instance. There is no sharedstatic
resource needing synchronization here. It looks thread safe.
– Elliott Frisch
Nov 27 '18 at 0:03
local variables are not shared between threads for non-static and static methods. What they reference could be.
– Peter Lawrey
Nov 27 '18 at 0:05
1
The second method does the same thing as the first method, but with named variables for some of the values. It doesn't change it's behaviour.
– Peter Lawrey
Nov 27 '18 at 0:06
|
show 2 more comments
I have this method that accepts an OkHttp#response
:
public static Map<String, Object> getResponseBody(Response response) throws IOException {
return new ObjectMapper()
.readValue(response.body().string(),
new TypeReference<Map<String, Object>>() {
});
}
From what I understand is that if multiple classes use the getResponseBody
, I'll encounter big problems as they'll all be accessing the same Response
.
Will it be solved if I do this?:
public static Map<String, Object> getResponseBody(Response response) throws IOException {
ResponseBody responseBody = response.body();
String responseString = responseBody.string();
ObjectMapper mapper = new ObjectMapper();
Map<String, Object> map = mapper
.readValue(responseString,
new TypeReference<Map<String, Object>>() {
});
return map;
}
java concurrency static
I have this method that accepts an OkHttp#response
:
public static Map<String, Object> getResponseBody(Response response) throws IOException {
return new ObjectMapper()
.readValue(response.body().string(),
new TypeReference<Map<String, Object>>() {
});
}
From what I understand is that if multiple classes use the getResponseBody
, I'll encounter big problems as they'll all be accessing the same Response
.
Will it be solved if I do this?:
public static Map<String, Object> getResponseBody(Response response) throws IOException {
ResponseBody responseBody = response.body();
String responseString = responseBody.string();
ObjectMapper mapper = new ObjectMapper();
Map<String, Object> map = mapper
.readValue(responseString,
new TypeReference<Map<String, Object>>() {
});
return map;
}
java concurrency static
java concurrency static
asked Nov 27 '18 at 0:00
Rigo SarmientoRigo Sarmiento
16110
16110
From what I understand is that if multiple classes use the getResponseBody, I'll encounter big problems as they'll all be accessing the same Response. That is not correct.
– Elliott Frisch
Nov 27 '18 at 0:01
Oh alright. May I ask for your correction?
– Rigo Sarmiento
Nov 27 '18 at 0:01
Every thread that calls thegetResponseBody
method will provide a thread localResponse
instance. There is no sharedstatic
resource needing synchronization here. It looks thread safe.
– Elliott Frisch
Nov 27 '18 at 0:03
local variables are not shared between threads for non-static and static methods. What they reference could be.
– Peter Lawrey
Nov 27 '18 at 0:05
1
The second method does the same thing as the first method, but with named variables for some of the values. It doesn't change it's behaviour.
– Peter Lawrey
Nov 27 '18 at 0:06
|
show 2 more comments
From what I understand is that if multiple classes use the getResponseBody, I'll encounter big problems as they'll all be accessing the same Response. That is not correct.
– Elliott Frisch
Nov 27 '18 at 0:01
Oh alright. May I ask for your correction?
– Rigo Sarmiento
Nov 27 '18 at 0:01
Every thread that calls thegetResponseBody
method will provide a thread localResponse
instance. There is no sharedstatic
resource needing synchronization here. It looks thread safe.
– Elliott Frisch
Nov 27 '18 at 0:03
local variables are not shared between threads for non-static and static methods. What they reference could be.
– Peter Lawrey
Nov 27 '18 at 0:05
1
The second method does the same thing as the first method, but with named variables for some of the values. It doesn't change it's behaviour.
– Peter Lawrey
Nov 27 '18 at 0:06
From what I understand is that if multiple classes use the getResponseBody, I'll encounter big problems as they'll all be accessing the same Response. That is not correct.
– Elliott Frisch
Nov 27 '18 at 0:01
From what I understand is that if multiple classes use the getResponseBody, I'll encounter big problems as they'll all be accessing the same Response. That is not correct.
– Elliott Frisch
Nov 27 '18 at 0:01
Oh alright. May I ask for your correction?
– Rigo Sarmiento
Nov 27 '18 at 0:01
Oh alright. May I ask for your correction?
– Rigo Sarmiento
Nov 27 '18 at 0:01
Every thread that calls the
getResponseBody
method will provide a thread local Response
instance. There is no shared static
resource needing synchronization here. It looks thread safe.– Elliott Frisch
Nov 27 '18 at 0:03
Every thread that calls the
getResponseBody
method will provide a thread local Response
instance. There is no shared static
resource needing synchronization here. It looks thread safe.– Elliott Frisch
Nov 27 '18 at 0:03
local variables are not shared between threads for non-static and static methods. What they reference could be.
– Peter Lawrey
Nov 27 '18 at 0:05
local variables are not shared between threads for non-static and static methods. What they reference could be.
– Peter Lawrey
Nov 27 '18 at 0:05
1
1
The second method does the same thing as the first method, but with named variables for some of the values. It doesn't change it's behaviour.
– Peter Lawrey
Nov 27 '18 at 0:06
The second method does the same thing as the first method, but with named variables for some of the values. It doesn't change it's behaviour.
– Peter Lawrey
Nov 27 '18 at 0:06
|
show 2 more comments
1 Answer
1
active
oldest
votes
As mentioned in the comments,
tl;dr: The first code I provided was thread safe all along.
Every thread that calls the getResponseBody method will provide a
thread local Response instance. There is no shared static resource
needing synchronization here. It looks thread safe.
Local variables are not shared between threads for non-static and
static methods. What they reference could be.
The second method does the same thing as the first method, but with
named variables for some of the values. It doesn't change it's
behaviour.
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53490877%2fwill-creating-new-objects-help-prevent-concurrency-issues-inside-a-static-method%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
As mentioned in the comments,
tl;dr: The first code I provided was thread safe all along.
Every thread that calls the getResponseBody method will provide a
thread local Response instance. There is no shared static resource
needing synchronization here. It looks thread safe.
Local variables are not shared between threads for non-static and
static methods. What they reference could be.
The second method does the same thing as the first method, but with
named variables for some of the values. It doesn't change it's
behaviour.
add a comment |
As mentioned in the comments,
tl;dr: The first code I provided was thread safe all along.
Every thread that calls the getResponseBody method will provide a
thread local Response instance. There is no shared static resource
needing synchronization here. It looks thread safe.
Local variables are not shared between threads for non-static and
static methods. What they reference could be.
The second method does the same thing as the first method, but with
named variables for some of the values. It doesn't change it's
behaviour.
add a comment |
As mentioned in the comments,
tl;dr: The first code I provided was thread safe all along.
Every thread that calls the getResponseBody method will provide a
thread local Response instance. There is no shared static resource
needing synchronization here. It looks thread safe.
Local variables are not shared between threads for non-static and
static methods. What they reference could be.
The second method does the same thing as the first method, but with
named variables for some of the values. It doesn't change it's
behaviour.
As mentioned in the comments,
tl;dr: The first code I provided was thread safe all along.
Every thread that calls the getResponseBody method will provide a
thread local Response instance. There is no shared static resource
needing synchronization here. It looks thread safe.
Local variables are not shared between threads for non-static and
static methods. What they reference could be.
The second method does the same thing as the first method, but with
named variables for some of the values. It doesn't change it's
behaviour.
answered Nov 27 '18 at 0:10
Rigo SarmientoRigo Sarmiento
16110
16110
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53490877%2fwill-creating-new-objects-help-prevent-concurrency-issues-inside-a-static-method%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
From what I understand is that if multiple classes use the getResponseBody, I'll encounter big problems as they'll all be accessing the same Response. That is not correct.
– Elliott Frisch
Nov 27 '18 at 0:01
Oh alright. May I ask for your correction?
– Rigo Sarmiento
Nov 27 '18 at 0:01
Every thread that calls the
getResponseBody
method will provide a thread localResponse
instance. There is no sharedstatic
resource needing synchronization here. It looks thread safe.– Elliott Frisch
Nov 27 '18 at 0:03
local variables are not shared between threads for non-static and static methods. What they reference could be.
– Peter Lawrey
Nov 27 '18 at 0:05
1
The second method does the same thing as the first method, but with named variables for some of the values. It doesn't change it's behaviour.
– Peter Lawrey
Nov 27 '18 at 0:06