ExecutorService argument to SwingWorker
I have an abstract class extending SwingWorker
called FetchWorker
to do some background data fetching and modifying the data that is fetched for my application. In my FetchWorker
is an inner class of StatisticLayerController
. This StatisticLayerController is extended by two classes. I initialize new threads within my FetchWorker
to do some calculations. I used to use the ExecutorService of TrackHistoryLayerController.
Like below:
public class TrackHistoryLayerController extends StatisticLayerController
{
private final ExecutorService heatMapAdderExecutor;
...
public AdsbFetchWorker(...) extends FetchWorker
{
super(...);
}
@Override
protected final List<ADSBTrack> doInBackground() throws Exception
{
filtered.forEach( track -> {
this.heatMapAdderExecutor.submit( new HeatmapAdderHelper( ... ) );
} );
while ( this.latch.getCount() != 0 )
{
this.publishValue( ( int ) this.latch.getCount() );
}
this.latch.await();
this.publishValue( ( int ) this.latch.getCount() );
if ( this.createImage() )
{
this.placeImage();
}
return filtered;
}
}
So in this case HeatMapAdderHelper
is my Helper thread which does some calculation for me. It was all working and all was fine.
But now I wanted to change class structure a bit more, I wanted to make my Controller
class abstract
and I no longer wanted my Worker
classes to be an inner class.
My question is, I cannot(should not) create ExecutorService
within a Worker
since the worker is going to be initialized each time event is invoked. Only thing I could do would be passing the ExecutorService from the Controller
as an argument to the SwingWorker
but would that be a good practice? Thank you in advance.
java multithreading swing executorservice swingworker
|
show 1 more comment
I have an abstract class extending SwingWorker
called FetchWorker
to do some background data fetching and modifying the data that is fetched for my application. In my FetchWorker
is an inner class of StatisticLayerController
. This StatisticLayerController is extended by two classes. I initialize new threads within my FetchWorker
to do some calculations. I used to use the ExecutorService of TrackHistoryLayerController.
Like below:
public class TrackHistoryLayerController extends StatisticLayerController
{
private final ExecutorService heatMapAdderExecutor;
...
public AdsbFetchWorker(...) extends FetchWorker
{
super(...);
}
@Override
protected final List<ADSBTrack> doInBackground() throws Exception
{
filtered.forEach( track -> {
this.heatMapAdderExecutor.submit( new HeatmapAdderHelper( ... ) );
} );
while ( this.latch.getCount() != 0 )
{
this.publishValue( ( int ) this.latch.getCount() );
}
this.latch.await();
this.publishValue( ( int ) this.latch.getCount() );
if ( this.createImage() )
{
this.placeImage();
}
return filtered;
}
}
So in this case HeatMapAdderHelper
is my Helper thread which does some calculation for me. It was all working and all was fine.
But now I wanted to change class structure a bit more, I wanted to make my Controller
class abstract
and I no longer wanted my Worker
classes to be an inner class.
My question is, I cannot(should not) create ExecutorService
within a Worker
since the worker is going to be initialized each time event is invoked. Only thing I could do would be passing the ExecutorService from the Controller
as an argument to the SwingWorker
but would that be a good practice? Thank you in advance.
java multithreading swing executorservice swingworker
Use astatic
executor or a singleton instance?
– daniu
Nov 23 '18 at 10:28
@daniu You mean within the worker right? But then Do not I have one ExecutorService for all of the controllers?
– Bleach
Nov 23 '18 at 10:33
Yes, you do. But do keep in mind that anExecutorService
is a wrapper that manages threads which are a global resource; it makes sense to manage them globally (and have only few of them). You don't get more performance magically by just adding threads (orExecutorService
s, for that matter).
– daniu
Nov 23 '18 at 10:35
@daniu So what you are saying is that havingExecutorService
for eachController
does not provide me some extra performance than having just oneExecutorService
for allControllers
?
– Bleach
Nov 23 '18 at 10:38
Yes, that's what I'm saying. Having 4ExecutorService
s with 1 thread each and having 1ExecutorService
with 1 thread are essentially the same performance-wise - however the latter will probably be better distributing the work.
– daniu
Nov 23 '18 at 10:46
|
show 1 more comment
I have an abstract class extending SwingWorker
called FetchWorker
to do some background data fetching and modifying the data that is fetched for my application. In my FetchWorker
is an inner class of StatisticLayerController
. This StatisticLayerController is extended by two classes. I initialize new threads within my FetchWorker
to do some calculations. I used to use the ExecutorService of TrackHistoryLayerController.
Like below:
public class TrackHistoryLayerController extends StatisticLayerController
{
private final ExecutorService heatMapAdderExecutor;
...
public AdsbFetchWorker(...) extends FetchWorker
{
super(...);
}
@Override
protected final List<ADSBTrack> doInBackground() throws Exception
{
filtered.forEach( track -> {
this.heatMapAdderExecutor.submit( new HeatmapAdderHelper( ... ) );
} );
while ( this.latch.getCount() != 0 )
{
this.publishValue( ( int ) this.latch.getCount() );
}
this.latch.await();
this.publishValue( ( int ) this.latch.getCount() );
if ( this.createImage() )
{
this.placeImage();
}
return filtered;
}
}
So in this case HeatMapAdderHelper
is my Helper thread which does some calculation for me. It was all working and all was fine.
But now I wanted to change class structure a bit more, I wanted to make my Controller
class abstract
and I no longer wanted my Worker
classes to be an inner class.
My question is, I cannot(should not) create ExecutorService
within a Worker
since the worker is going to be initialized each time event is invoked. Only thing I could do would be passing the ExecutorService from the Controller
as an argument to the SwingWorker
but would that be a good practice? Thank you in advance.
java multithreading swing executorservice swingworker
I have an abstract class extending SwingWorker
called FetchWorker
to do some background data fetching and modifying the data that is fetched for my application. In my FetchWorker
is an inner class of StatisticLayerController
. This StatisticLayerController is extended by two classes. I initialize new threads within my FetchWorker
to do some calculations. I used to use the ExecutorService of TrackHistoryLayerController.
Like below:
public class TrackHistoryLayerController extends StatisticLayerController
{
private final ExecutorService heatMapAdderExecutor;
...
public AdsbFetchWorker(...) extends FetchWorker
{
super(...);
}
@Override
protected final List<ADSBTrack> doInBackground() throws Exception
{
filtered.forEach( track -> {
this.heatMapAdderExecutor.submit( new HeatmapAdderHelper( ... ) );
} );
while ( this.latch.getCount() != 0 )
{
this.publishValue( ( int ) this.latch.getCount() );
}
this.latch.await();
this.publishValue( ( int ) this.latch.getCount() );
if ( this.createImage() )
{
this.placeImage();
}
return filtered;
}
}
So in this case HeatMapAdderHelper
is my Helper thread which does some calculation for me. It was all working and all was fine.
But now I wanted to change class structure a bit more, I wanted to make my Controller
class abstract
and I no longer wanted my Worker
classes to be an inner class.
My question is, I cannot(should not) create ExecutorService
within a Worker
since the worker is going to be initialized each time event is invoked. Only thing I could do would be passing the ExecutorService from the Controller
as an argument to the SwingWorker
but would that be a good practice? Thank you in advance.
java multithreading swing executorservice swingworker
java multithreading swing executorservice swingworker
asked Nov 23 '18 at 10:26
BleachBleach
91111
91111
Use astatic
executor or a singleton instance?
– daniu
Nov 23 '18 at 10:28
@daniu You mean within the worker right? But then Do not I have one ExecutorService for all of the controllers?
– Bleach
Nov 23 '18 at 10:33
Yes, you do. But do keep in mind that anExecutorService
is a wrapper that manages threads which are a global resource; it makes sense to manage them globally (and have only few of them). You don't get more performance magically by just adding threads (orExecutorService
s, for that matter).
– daniu
Nov 23 '18 at 10:35
@daniu So what you are saying is that havingExecutorService
for eachController
does not provide me some extra performance than having just oneExecutorService
for allControllers
?
– Bleach
Nov 23 '18 at 10:38
Yes, that's what I'm saying. Having 4ExecutorService
s with 1 thread each and having 1ExecutorService
with 1 thread are essentially the same performance-wise - however the latter will probably be better distributing the work.
– daniu
Nov 23 '18 at 10:46
|
show 1 more comment
Use astatic
executor or a singleton instance?
– daniu
Nov 23 '18 at 10:28
@daniu You mean within the worker right? But then Do not I have one ExecutorService for all of the controllers?
– Bleach
Nov 23 '18 at 10:33
Yes, you do. But do keep in mind that anExecutorService
is a wrapper that manages threads which are a global resource; it makes sense to manage them globally (and have only few of them). You don't get more performance magically by just adding threads (orExecutorService
s, for that matter).
– daniu
Nov 23 '18 at 10:35
@daniu So what you are saying is that havingExecutorService
for eachController
does not provide me some extra performance than having just oneExecutorService
for allControllers
?
– Bleach
Nov 23 '18 at 10:38
Yes, that's what I'm saying. Having 4ExecutorService
s with 1 thread each and having 1ExecutorService
with 1 thread are essentially the same performance-wise - however the latter will probably be better distributing the work.
– daniu
Nov 23 '18 at 10:46
Use a
static
executor or a singleton instance?– daniu
Nov 23 '18 at 10:28
Use a
static
executor or a singleton instance?– daniu
Nov 23 '18 at 10:28
@daniu You mean within the worker right? But then Do not I have one ExecutorService for all of the controllers?
– Bleach
Nov 23 '18 at 10:33
@daniu You mean within the worker right? But then Do not I have one ExecutorService for all of the controllers?
– Bleach
Nov 23 '18 at 10:33
Yes, you do. But do keep in mind that an
ExecutorService
is a wrapper that manages threads which are a global resource; it makes sense to manage them globally (and have only few of them). You don't get more performance magically by just adding threads (or ExecutorService
s, for that matter).– daniu
Nov 23 '18 at 10:35
Yes, you do. But do keep in mind that an
ExecutorService
is a wrapper that manages threads which are a global resource; it makes sense to manage them globally (and have only few of them). You don't get more performance magically by just adding threads (or ExecutorService
s, for that matter).– daniu
Nov 23 '18 at 10:35
@daniu So what you are saying is that having
ExecutorService
for each Controller
does not provide me some extra performance than having just one ExecutorService
for all Controllers
?– Bleach
Nov 23 '18 at 10:38
@daniu So what you are saying is that having
ExecutorService
for each Controller
does not provide me some extra performance than having just one ExecutorService
for all Controllers
?– Bleach
Nov 23 '18 at 10:38
Yes, that's what I'm saying. Having 4
ExecutorService
s with 1 thread each and having 1 ExecutorService
with 1 thread are essentially the same performance-wise - however the latter will probably be better distributing the work.– daniu
Nov 23 '18 at 10:46
Yes, that's what I'm saying. Having 4
ExecutorService
s with 1 thread each and having 1 ExecutorService
with 1 thread are essentially the same performance-wise - however the latter will probably be better distributing the work.– daniu
Nov 23 '18 at 10:46
|
show 1 more comment
1 Answer
1
active
oldest
votes
You can of course also just use the default ExecutorService
provided by CompletableFuture
by doing
filtered.forEach( track -> {
CompletableFuture.runAsync(new HeatmapAdderHelper( ... ) );
} );
As a side note, this
while ( this.latch.getCount() != 0 )
{
this.publishValue( ( int ) this.latch.getCount() );
}
this.latch.await();
this.publishValue( ( int ) this.latch.getCount() );
looks very dodgy... a busy loop while waiting for the results? Also, why do you await
the latch after you already waited in the loop? Assuming the latch is a CountDownLatch
.
I'm sure if you provided even more context, we could give a better overall solution.
It looks like you can just do something like
CompletableFuture<Void> futures =
filtered.stream().map(t -> new HeatmapAdderHelper(t))
.map(CompletableFuture::runAsync)
.toArray(CompletableFuture::new);
CompletableFuture.allOf(futures).andThen(createImage());
Or with a CountDownLatch
:
CountDownLatch latch = new CountDownLatch(filtered.size());
filtered.forEach(f -> {
CompletableFuture.runAsync(new HeatmapAdderHelper(f))
.thenRun(latch::countDown);
});
latch.await();
createImage();
I think I was a bit careless not to see thatwhile-loop
problem, it was just intended to view the progress, thank you for pointing out, I will modify it. Although I am really not familiar withCompletableFuture
, in fact I never used it before. In this case how is it different from theExecutorService
? Does it have initial pool size?
– Bleach
Nov 23 '18 at 11:14
@Bleach ACompletableFuture
is a result that is being calculated concurrently; it implements theFuture
interface (you get one of those when yousubmit()
aCallable
to anExecutorService
. TheCompletableFuture#runAsync
is pretty much just a convenience method to run something in the background without taking care of anExecutorService
to manage the threads.
– daniu
Nov 23 '18 at 11:39
I modified my code in the way you wrote. But I notice everytime I click on my button to calculate, just one of the threads are gets running in JVM, like in the photo in this photo Is that normal?
– Bleach
Nov 23 '18 at 13:35
@Bleach I really can't tell you that; you might want to look into why the other threads are sleeping/locked etc.
– daniu
Nov 23 '18 at 14:09
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%2f53444904%2fexecutorservice-argument-to-swingworker%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
You can of course also just use the default ExecutorService
provided by CompletableFuture
by doing
filtered.forEach( track -> {
CompletableFuture.runAsync(new HeatmapAdderHelper( ... ) );
} );
As a side note, this
while ( this.latch.getCount() != 0 )
{
this.publishValue( ( int ) this.latch.getCount() );
}
this.latch.await();
this.publishValue( ( int ) this.latch.getCount() );
looks very dodgy... a busy loop while waiting for the results? Also, why do you await
the latch after you already waited in the loop? Assuming the latch is a CountDownLatch
.
I'm sure if you provided even more context, we could give a better overall solution.
It looks like you can just do something like
CompletableFuture<Void> futures =
filtered.stream().map(t -> new HeatmapAdderHelper(t))
.map(CompletableFuture::runAsync)
.toArray(CompletableFuture::new);
CompletableFuture.allOf(futures).andThen(createImage());
Or with a CountDownLatch
:
CountDownLatch latch = new CountDownLatch(filtered.size());
filtered.forEach(f -> {
CompletableFuture.runAsync(new HeatmapAdderHelper(f))
.thenRun(latch::countDown);
});
latch.await();
createImage();
I think I was a bit careless not to see thatwhile-loop
problem, it was just intended to view the progress, thank you for pointing out, I will modify it. Although I am really not familiar withCompletableFuture
, in fact I never used it before. In this case how is it different from theExecutorService
? Does it have initial pool size?
– Bleach
Nov 23 '18 at 11:14
@Bleach ACompletableFuture
is a result that is being calculated concurrently; it implements theFuture
interface (you get one of those when yousubmit()
aCallable
to anExecutorService
. TheCompletableFuture#runAsync
is pretty much just a convenience method to run something in the background without taking care of anExecutorService
to manage the threads.
– daniu
Nov 23 '18 at 11:39
I modified my code in the way you wrote. But I notice everytime I click on my button to calculate, just one of the threads are gets running in JVM, like in the photo in this photo Is that normal?
– Bleach
Nov 23 '18 at 13:35
@Bleach I really can't tell you that; you might want to look into why the other threads are sleeping/locked etc.
– daniu
Nov 23 '18 at 14:09
add a comment |
You can of course also just use the default ExecutorService
provided by CompletableFuture
by doing
filtered.forEach( track -> {
CompletableFuture.runAsync(new HeatmapAdderHelper( ... ) );
} );
As a side note, this
while ( this.latch.getCount() != 0 )
{
this.publishValue( ( int ) this.latch.getCount() );
}
this.latch.await();
this.publishValue( ( int ) this.latch.getCount() );
looks very dodgy... a busy loop while waiting for the results? Also, why do you await
the latch after you already waited in the loop? Assuming the latch is a CountDownLatch
.
I'm sure if you provided even more context, we could give a better overall solution.
It looks like you can just do something like
CompletableFuture<Void> futures =
filtered.stream().map(t -> new HeatmapAdderHelper(t))
.map(CompletableFuture::runAsync)
.toArray(CompletableFuture::new);
CompletableFuture.allOf(futures).andThen(createImage());
Or with a CountDownLatch
:
CountDownLatch latch = new CountDownLatch(filtered.size());
filtered.forEach(f -> {
CompletableFuture.runAsync(new HeatmapAdderHelper(f))
.thenRun(latch::countDown);
});
latch.await();
createImage();
I think I was a bit careless not to see thatwhile-loop
problem, it was just intended to view the progress, thank you for pointing out, I will modify it. Although I am really not familiar withCompletableFuture
, in fact I never used it before. In this case how is it different from theExecutorService
? Does it have initial pool size?
– Bleach
Nov 23 '18 at 11:14
@Bleach ACompletableFuture
is a result that is being calculated concurrently; it implements theFuture
interface (you get one of those when yousubmit()
aCallable
to anExecutorService
. TheCompletableFuture#runAsync
is pretty much just a convenience method to run something in the background without taking care of anExecutorService
to manage the threads.
– daniu
Nov 23 '18 at 11:39
I modified my code in the way you wrote. But I notice everytime I click on my button to calculate, just one of the threads are gets running in JVM, like in the photo in this photo Is that normal?
– Bleach
Nov 23 '18 at 13:35
@Bleach I really can't tell you that; you might want to look into why the other threads are sleeping/locked etc.
– daniu
Nov 23 '18 at 14:09
add a comment |
You can of course also just use the default ExecutorService
provided by CompletableFuture
by doing
filtered.forEach( track -> {
CompletableFuture.runAsync(new HeatmapAdderHelper( ... ) );
} );
As a side note, this
while ( this.latch.getCount() != 0 )
{
this.publishValue( ( int ) this.latch.getCount() );
}
this.latch.await();
this.publishValue( ( int ) this.latch.getCount() );
looks very dodgy... a busy loop while waiting for the results? Also, why do you await
the latch after you already waited in the loop? Assuming the latch is a CountDownLatch
.
I'm sure if you provided even more context, we could give a better overall solution.
It looks like you can just do something like
CompletableFuture<Void> futures =
filtered.stream().map(t -> new HeatmapAdderHelper(t))
.map(CompletableFuture::runAsync)
.toArray(CompletableFuture::new);
CompletableFuture.allOf(futures).andThen(createImage());
Or with a CountDownLatch
:
CountDownLatch latch = new CountDownLatch(filtered.size());
filtered.forEach(f -> {
CompletableFuture.runAsync(new HeatmapAdderHelper(f))
.thenRun(latch::countDown);
});
latch.await();
createImage();
You can of course also just use the default ExecutorService
provided by CompletableFuture
by doing
filtered.forEach( track -> {
CompletableFuture.runAsync(new HeatmapAdderHelper( ... ) );
} );
As a side note, this
while ( this.latch.getCount() != 0 )
{
this.publishValue( ( int ) this.latch.getCount() );
}
this.latch.await();
this.publishValue( ( int ) this.latch.getCount() );
looks very dodgy... a busy loop while waiting for the results? Also, why do you await
the latch after you already waited in the loop? Assuming the latch is a CountDownLatch
.
I'm sure if you provided even more context, we could give a better overall solution.
It looks like you can just do something like
CompletableFuture<Void> futures =
filtered.stream().map(t -> new HeatmapAdderHelper(t))
.map(CompletableFuture::runAsync)
.toArray(CompletableFuture::new);
CompletableFuture.allOf(futures).andThen(createImage());
Or with a CountDownLatch
:
CountDownLatch latch = new CountDownLatch(filtered.size());
filtered.forEach(f -> {
CompletableFuture.runAsync(new HeatmapAdderHelper(f))
.thenRun(latch::countDown);
});
latch.await();
createImage();
edited Nov 23 '18 at 12:07
answered Nov 23 '18 at 10:57
daniudaniu
7,46521635
7,46521635
I think I was a bit careless not to see thatwhile-loop
problem, it was just intended to view the progress, thank you for pointing out, I will modify it. Although I am really not familiar withCompletableFuture
, in fact I never used it before. In this case how is it different from theExecutorService
? Does it have initial pool size?
– Bleach
Nov 23 '18 at 11:14
@Bleach ACompletableFuture
is a result that is being calculated concurrently; it implements theFuture
interface (you get one of those when yousubmit()
aCallable
to anExecutorService
. TheCompletableFuture#runAsync
is pretty much just a convenience method to run something in the background without taking care of anExecutorService
to manage the threads.
– daniu
Nov 23 '18 at 11:39
I modified my code in the way you wrote. But I notice everytime I click on my button to calculate, just one of the threads are gets running in JVM, like in the photo in this photo Is that normal?
– Bleach
Nov 23 '18 at 13:35
@Bleach I really can't tell you that; you might want to look into why the other threads are sleeping/locked etc.
– daniu
Nov 23 '18 at 14:09
add a comment |
I think I was a bit careless not to see thatwhile-loop
problem, it was just intended to view the progress, thank you for pointing out, I will modify it. Although I am really not familiar withCompletableFuture
, in fact I never used it before. In this case how is it different from theExecutorService
? Does it have initial pool size?
– Bleach
Nov 23 '18 at 11:14
@Bleach ACompletableFuture
is a result that is being calculated concurrently; it implements theFuture
interface (you get one of those when yousubmit()
aCallable
to anExecutorService
. TheCompletableFuture#runAsync
is pretty much just a convenience method to run something in the background without taking care of anExecutorService
to manage the threads.
– daniu
Nov 23 '18 at 11:39
I modified my code in the way you wrote. But I notice everytime I click on my button to calculate, just one of the threads are gets running in JVM, like in the photo in this photo Is that normal?
– Bleach
Nov 23 '18 at 13:35
@Bleach I really can't tell you that; you might want to look into why the other threads are sleeping/locked etc.
– daniu
Nov 23 '18 at 14:09
I think I was a bit careless not to see that
while-loop
problem, it was just intended to view the progress, thank you for pointing out, I will modify it. Although I am really not familiar with CompletableFuture
, in fact I never used it before. In this case how is it different from the ExecutorService
? Does it have initial pool size?– Bleach
Nov 23 '18 at 11:14
I think I was a bit careless not to see that
while-loop
problem, it was just intended to view the progress, thank you for pointing out, I will modify it. Although I am really not familiar with CompletableFuture
, in fact I never used it before. In this case how is it different from the ExecutorService
? Does it have initial pool size?– Bleach
Nov 23 '18 at 11:14
@Bleach A
CompletableFuture
is a result that is being calculated concurrently; it implements the Future
interface (you get one of those when you submit()
a Callable
to an ExecutorService
. The CompletableFuture#runAsync
is pretty much just a convenience method to run something in the background without taking care of an ExecutorService
to manage the threads.– daniu
Nov 23 '18 at 11:39
@Bleach A
CompletableFuture
is a result that is being calculated concurrently; it implements the Future
interface (you get one of those when you submit()
a Callable
to an ExecutorService
. The CompletableFuture#runAsync
is pretty much just a convenience method to run something in the background without taking care of an ExecutorService
to manage the threads.– daniu
Nov 23 '18 at 11:39
I modified my code in the way you wrote. But I notice everytime I click on my button to calculate, just one of the threads are gets running in JVM, like in the photo in this photo Is that normal?
– Bleach
Nov 23 '18 at 13:35
I modified my code in the way you wrote. But I notice everytime I click on my button to calculate, just one of the threads are gets running in JVM, like in the photo in this photo Is that normal?
– Bleach
Nov 23 '18 at 13:35
@Bleach I really can't tell you that; you might want to look into why the other threads are sleeping/locked etc.
– daniu
Nov 23 '18 at 14:09
@Bleach I really can't tell you that; you might want to look into why the other threads are sleeping/locked etc.
– daniu
Nov 23 '18 at 14:09
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%2f53444904%2fexecutorservice-argument-to-swingworker%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
Use a
static
executor or a singleton instance?– daniu
Nov 23 '18 at 10:28
@daniu You mean within the worker right? But then Do not I have one ExecutorService for all of the controllers?
– Bleach
Nov 23 '18 at 10:33
Yes, you do. But do keep in mind that an
ExecutorService
is a wrapper that manages threads which are a global resource; it makes sense to manage them globally (and have only few of them). You don't get more performance magically by just adding threads (orExecutorService
s, for that matter).– daniu
Nov 23 '18 at 10:35
@daniu So what you are saying is that having
ExecutorService
for eachController
does not provide me some extra performance than having just oneExecutorService
for allControllers
?– Bleach
Nov 23 '18 at 10:38
Yes, that's what I'm saying. Having 4
ExecutorService
s with 1 thread each and having 1ExecutorService
with 1 thread are essentially the same performance-wise - however the latter will probably be better distributing the work.– daniu
Nov 23 '18 at 10:46