Approach to writing ExecutorService for updating AtomicBoolean value
I'm asking this question specific to this use case, as to what is the best as in most cost-efficient and elegant way to achieve the following:
public class MyClass {
private final AtomicBoolean _myValue = new AtomicBoolean(false);
public MyClass(MyApi someApi, Executor executor) {
_someApi = someApi;
_executor = executor;
}
public startSession() {
updateValue();
}
public sessionEnded() {
//Do nothing
}
public boolean getMyValue() {
return _myValue.get();
}
//the magic should happen here
private void updateValue() {
_executor.execute(() => {
_myValue.set(processValue(_someApi.getValue()));
});
}
//some processing. not that relevant.
private boolean processValue(Values values) {
try {
for (...) {... return true;}
} catch (..) { }
return false;
}
}
The scenario:
- on each user login startedSession is called, meaning it could be 10 users, or 100 users, calling startedSession at the same time. The key here is they're all calling set on _myValue. Meanwhile anyone else can be calling get.
- and then for the specific handling:
Options I see:
1)
Implementation-wise let the API consumer have control over the executor size, by passing it through constructor, as done in the example.
2)
Create Executor from within class.
Example:
MyClass() {
_executor = Executors.newFixedThreadPool(10);
}
Calling update to _myValue, isn't expected to happen a lot at once, or with frequent changes for _myValue, but still I want to follow good practices.
Options I see:
1) If update is called and running - cancel all current incoming task(update). Once it's finished - accept first incoming and again cancel next ones until current is done, etc. ...
2) Queue them and not cancel if one is in progress.
Any suggestions on which approach is better?
Is it so expensive to queue them, or am I digging too much?
And one last question: how can I make sure my Executor gets shut down and garbage collected without shutting it down while there can still be incoming updating requests?
Open for any suggestions and tips and more options.
java multithreading runnable executorservice
|
show 2 more comments
I'm asking this question specific to this use case, as to what is the best as in most cost-efficient and elegant way to achieve the following:
public class MyClass {
private final AtomicBoolean _myValue = new AtomicBoolean(false);
public MyClass(MyApi someApi, Executor executor) {
_someApi = someApi;
_executor = executor;
}
public startSession() {
updateValue();
}
public sessionEnded() {
//Do nothing
}
public boolean getMyValue() {
return _myValue.get();
}
//the magic should happen here
private void updateValue() {
_executor.execute(() => {
_myValue.set(processValue(_someApi.getValue()));
});
}
//some processing. not that relevant.
private boolean processValue(Values values) {
try {
for (...) {... return true;}
} catch (..) { }
return false;
}
}
The scenario:
- on each user login startedSession is called, meaning it could be 10 users, or 100 users, calling startedSession at the same time. The key here is they're all calling set on _myValue. Meanwhile anyone else can be calling get.
- and then for the specific handling:
Options I see:
1)
Implementation-wise let the API consumer have control over the executor size, by passing it through constructor, as done in the example.
2)
Create Executor from within class.
Example:
MyClass() {
_executor = Executors.newFixedThreadPool(10);
}
Calling update to _myValue, isn't expected to happen a lot at once, or with frequent changes for _myValue, but still I want to follow good practices.
Options I see:
1) If update is called and running - cancel all current incoming task(update). Once it's finished - accept first incoming and again cancel next ones until current is done, etc. ...
2) Queue them and not cancel if one is in progress.
Any suggestions on which approach is better?
Is it so expensive to queue them, or am I digging too much?
And one last question: how can I make sure my Executor gets shut down and garbage collected without shutting it down while there can still be incoming updating requests?
Open for any suggestions and tips and more options.
java multithreading runnable executorservice
1
You haven't said what you wanted to achieve in the first place. You have options, but options to do what? Besides, you said "the key here is they're all calling set on _myValue", but you're never calling set anywhere.
– JB Nizet
Nov 25 '18 at 11:24
I am: from within updateValue: _myValue = processValue(_someApi.getValue()); What I'm thrying to achieve is to handle set and get of _myValue in a concurrent way, so that the most updated value is sure to be sent to each user.
– JJey
Nov 25 '18 at 11:30
That doesn't call any set() method. Just read your code. Where do you seeset(...)?
– JB Nizet
Nov 25 '18 at 11:31
Edited the code - it's calling set on the AtmocBoolean.
– JJey
Nov 25 '18 at 11:41
You still haven't said what you want to achieve. It's hard to advise a solution without knowing the problem to solve.
– JB Nizet
Nov 25 '18 at 11:42
|
show 2 more comments
I'm asking this question specific to this use case, as to what is the best as in most cost-efficient and elegant way to achieve the following:
public class MyClass {
private final AtomicBoolean _myValue = new AtomicBoolean(false);
public MyClass(MyApi someApi, Executor executor) {
_someApi = someApi;
_executor = executor;
}
public startSession() {
updateValue();
}
public sessionEnded() {
//Do nothing
}
public boolean getMyValue() {
return _myValue.get();
}
//the magic should happen here
private void updateValue() {
_executor.execute(() => {
_myValue.set(processValue(_someApi.getValue()));
});
}
//some processing. not that relevant.
private boolean processValue(Values values) {
try {
for (...) {... return true;}
} catch (..) { }
return false;
}
}
The scenario:
- on each user login startedSession is called, meaning it could be 10 users, or 100 users, calling startedSession at the same time. The key here is they're all calling set on _myValue. Meanwhile anyone else can be calling get.
- and then for the specific handling:
Options I see:
1)
Implementation-wise let the API consumer have control over the executor size, by passing it through constructor, as done in the example.
2)
Create Executor from within class.
Example:
MyClass() {
_executor = Executors.newFixedThreadPool(10);
}
Calling update to _myValue, isn't expected to happen a lot at once, or with frequent changes for _myValue, but still I want to follow good practices.
Options I see:
1) If update is called and running - cancel all current incoming task(update). Once it's finished - accept first incoming and again cancel next ones until current is done, etc. ...
2) Queue them and not cancel if one is in progress.
Any suggestions on which approach is better?
Is it so expensive to queue them, or am I digging too much?
And one last question: how can I make sure my Executor gets shut down and garbage collected without shutting it down while there can still be incoming updating requests?
Open for any suggestions and tips and more options.
java multithreading runnable executorservice
I'm asking this question specific to this use case, as to what is the best as in most cost-efficient and elegant way to achieve the following:
public class MyClass {
private final AtomicBoolean _myValue = new AtomicBoolean(false);
public MyClass(MyApi someApi, Executor executor) {
_someApi = someApi;
_executor = executor;
}
public startSession() {
updateValue();
}
public sessionEnded() {
//Do nothing
}
public boolean getMyValue() {
return _myValue.get();
}
//the magic should happen here
private void updateValue() {
_executor.execute(() => {
_myValue.set(processValue(_someApi.getValue()));
});
}
//some processing. not that relevant.
private boolean processValue(Values values) {
try {
for (...) {... return true;}
} catch (..) { }
return false;
}
}
The scenario:
- on each user login startedSession is called, meaning it could be 10 users, or 100 users, calling startedSession at the same time. The key here is they're all calling set on _myValue. Meanwhile anyone else can be calling get.
- and then for the specific handling:
Options I see:
1)
Implementation-wise let the API consumer have control over the executor size, by passing it through constructor, as done in the example.
2)
Create Executor from within class.
Example:
MyClass() {
_executor = Executors.newFixedThreadPool(10);
}
Calling update to _myValue, isn't expected to happen a lot at once, or with frequent changes for _myValue, but still I want to follow good practices.
Options I see:
1) If update is called and running - cancel all current incoming task(update). Once it's finished - accept first incoming and again cancel next ones until current is done, etc. ...
2) Queue them and not cancel if one is in progress.
Any suggestions on which approach is better?
Is it so expensive to queue them, or am I digging too much?
And one last question: how can I make sure my Executor gets shut down and garbage collected without shutting it down while there can still be incoming updating requests?
Open for any suggestions and tips and more options.
java multithreading runnable executorservice
java multithreading runnable executorservice
edited Nov 25 '18 at 11:40
JJey
asked Nov 25 '18 at 11:19
JJeyJJey
5710
5710
1
You haven't said what you wanted to achieve in the first place. You have options, but options to do what? Besides, you said "the key here is they're all calling set on _myValue", but you're never calling set anywhere.
– JB Nizet
Nov 25 '18 at 11:24
I am: from within updateValue: _myValue = processValue(_someApi.getValue()); What I'm thrying to achieve is to handle set and get of _myValue in a concurrent way, so that the most updated value is sure to be sent to each user.
– JJey
Nov 25 '18 at 11:30
That doesn't call any set() method. Just read your code. Where do you seeset(...)?
– JB Nizet
Nov 25 '18 at 11:31
Edited the code - it's calling set on the AtmocBoolean.
– JJey
Nov 25 '18 at 11:41
You still haven't said what you want to achieve. It's hard to advise a solution without knowing the problem to solve.
– JB Nizet
Nov 25 '18 at 11:42
|
show 2 more comments
1
You haven't said what you wanted to achieve in the first place. You have options, but options to do what? Besides, you said "the key here is they're all calling set on _myValue", but you're never calling set anywhere.
– JB Nizet
Nov 25 '18 at 11:24
I am: from within updateValue: _myValue = processValue(_someApi.getValue()); What I'm thrying to achieve is to handle set and get of _myValue in a concurrent way, so that the most updated value is sure to be sent to each user.
– JJey
Nov 25 '18 at 11:30
That doesn't call any set() method. Just read your code. Where do you seeset(...)?
– JB Nizet
Nov 25 '18 at 11:31
Edited the code - it's calling set on the AtmocBoolean.
– JJey
Nov 25 '18 at 11:41
You still haven't said what you want to achieve. It's hard to advise a solution without knowing the problem to solve.
– JB Nizet
Nov 25 '18 at 11:42
1
1
You haven't said what you wanted to achieve in the first place. You have options, but options to do what? Besides, you said "the key here is they're all calling set on _myValue", but you're never calling set anywhere.
– JB Nizet
Nov 25 '18 at 11:24
You haven't said what you wanted to achieve in the first place. You have options, but options to do what? Besides, you said "the key here is they're all calling set on _myValue", but you're never calling set anywhere.
– JB Nizet
Nov 25 '18 at 11:24
I am: from within updateValue: _myValue = processValue(_someApi.getValue()); What I'm thrying to achieve is to handle set and get of _myValue in a concurrent way, so that the most updated value is sure to be sent to each user.
– JJey
Nov 25 '18 at 11:30
I am: from within updateValue: _myValue = processValue(_someApi.getValue()); What I'm thrying to achieve is to handle set and get of _myValue in a concurrent way, so that the most updated value is sure to be sent to each user.
– JJey
Nov 25 '18 at 11:30
That doesn't call any set() method. Just read your code. Where do you see
set(...)?– JB Nizet
Nov 25 '18 at 11:31
That doesn't call any set() method. Just read your code. Where do you see
set(...)?– JB Nizet
Nov 25 '18 at 11:31
Edited the code - it's calling set on the AtmocBoolean.
– JJey
Nov 25 '18 at 11:41
Edited the code - it's calling set on the AtmocBoolean.
– JJey
Nov 25 '18 at 11:41
You still haven't said what you want to achieve. It's hard to advise a solution without knowing the problem to solve.
– JB Nizet
Nov 25 '18 at 11:42
You still haven't said what you want to achieve. It's hard to advise a solution without knowing the problem to solve.
– JB Nizet
Nov 25 '18 at 11:42
|
show 2 more comments
0
active
oldest
votes
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%2f53466917%2fapproach-to-writing-executorservice-for-updating-atomicboolean-value%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
0
active
oldest
votes
0
active
oldest
votes
active
oldest
votes
active
oldest
votes
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%2f53466917%2fapproach-to-writing-executorservice-for-updating-atomicboolean-value%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
1
You haven't said what you wanted to achieve in the first place. You have options, but options to do what? Besides, you said "the key here is they're all calling set on _myValue", but you're never calling set anywhere.
– JB Nizet
Nov 25 '18 at 11:24
I am: from within updateValue: _myValue = processValue(_someApi.getValue()); What I'm thrying to achieve is to handle set and get of _myValue in a concurrent way, so that the most updated value is sure to be sent to each user.
– JJey
Nov 25 '18 at 11:30
That doesn't call any set() method. Just read your code. Where do you see
set(...)?– JB Nizet
Nov 25 '18 at 11:31
Edited the code - it's calling set on the AtmocBoolean.
– JJey
Nov 25 '18 at 11:41
You still haven't said what you want to achieve. It's hard to advise a solution without knowing the problem to solve.
– JB Nizet
Nov 25 '18 at 11:42