JavaFX: Is it a good idea to save threads in a collection? and how to do that?
up vote
0
down vote
favorite
I have several buttons, each of them, when pressed, creates a Thread.
I want that if pressed again, a button stops its process.
Would it be a good idea to store them in a structure like a collection? or is it stupid?
Thanks
multithreading javafx collections runnable
add a comment |
up vote
0
down vote
favorite
I have several buttons, each of them, when pressed, creates a Thread.
I want that if pressed again, a button stops its process.
Would it be a good idea to store them in a structure like a collection? or is it stupid?
Thanks
multithreading javafx collections runnable
You wouldn't normally store the actualThread
reference. Instead, you'd store theFuture
(obtained by submitting the task to anExecutorService
) or the more JavaFX specificWorker
reference. When you press theButton
again you'd get the correct reference and call the correspondingcancel
method. Note: Cancelling background tasks requires cooperation from the developer.
– Slaw
Nov 18 at 9:02
To add to/clarify what Slaw said - creatingThread
s haphazardly is usually not a good idea, as you may starve the system of resources - threads aren't free, and can and should be reused. Using anExecutorService
will help you make sure you're not creating too much threads and you are reusing threads when possible.
– Itai
Nov 18 at 9:18
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I have several buttons, each of them, when pressed, creates a Thread.
I want that if pressed again, a button stops its process.
Would it be a good idea to store them in a structure like a collection? or is it stupid?
Thanks
multithreading javafx collections runnable
I have several buttons, each of them, when pressed, creates a Thread.
I want that if pressed again, a button stops its process.
Would it be a good idea to store them in a structure like a collection? or is it stupid?
Thanks
multithreading javafx collections runnable
multithreading javafx collections runnable
asked Nov 18 at 7:56
bog
52031121
52031121
You wouldn't normally store the actualThread
reference. Instead, you'd store theFuture
(obtained by submitting the task to anExecutorService
) or the more JavaFX specificWorker
reference. When you press theButton
again you'd get the correct reference and call the correspondingcancel
method. Note: Cancelling background tasks requires cooperation from the developer.
– Slaw
Nov 18 at 9:02
To add to/clarify what Slaw said - creatingThread
s haphazardly is usually not a good idea, as you may starve the system of resources - threads aren't free, and can and should be reused. Using anExecutorService
will help you make sure you're not creating too much threads and you are reusing threads when possible.
– Itai
Nov 18 at 9:18
add a comment |
You wouldn't normally store the actualThread
reference. Instead, you'd store theFuture
(obtained by submitting the task to anExecutorService
) or the more JavaFX specificWorker
reference. When you press theButton
again you'd get the correct reference and call the correspondingcancel
method. Note: Cancelling background tasks requires cooperation from the developer.
– Slaw
Nov 18 at 9:02
To add to/clarify what Slaw said - creatingThread
s haphazardly is usually not a good idea, as you may starve the system of resources - threads aren't free, and can and should be reused. Using anExecutorService
will help you make sure you're not creating too much threads and you are reusing threads when possible.
– Itai
Nov 18 at 9:18
You wouldn't normally store the actual
Thread
reference. Instead, you'd store the Future
(obtained by submitting the task to an ExecutorService
) or the more JavaFX specific Worker
reference. When you press the Button
again you'd get the correct reference and call the corresponding cancel
method. Note: Cancelling background tasks requires cooperation from the developer.– Slaw
Nov 18 at 9:02
You wouldn't normally store the actual
Thread
reference. Instead, you'd store the Future
(obtained by submitting the task to an ExecutorService
) or the more JavaFX specific Worker
reference. When you press the Button
again you'd get the correct reference and call the corresponding cancel
method. Note: Cancelling background tasks requires cooperation from the developer.– Slaw
Nov 18 at 9:02
To add to/clarify what Slaw said - creating
Thread
s haphazardly is usually not a good idea, as you may starve the system of resources - threads aren't free, and can and should be reused. Using an ExecutorService
will help you make sure you're not creating too much threads and you are reusing threads when possible.– Itai
Nov 18 at 9:18
To add to/clarify what Slaw said - creating
Thread
s haphazardly is usually not a good idea, as you may starve the system of resources - threads aren't free, and can and should be reused. Using an ExecutorService
will help you make sure you're not creating too much threads and you are reusing threads when possible.– Itai
Nov 18 at 9:18
add a comment |
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
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%2f53358918%2fjavafx-is-it-a-good-idea-to-save-threads-in-a-collection-and-how-to-do-that%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
You wouldn't normally store the actual
Thread
reference. Instead, you'd store theFuture
(obtained by submitting the task to anExecutorService
) or the more JavaFX specificWorker
reference. When you press theButton
again you'd get the correct reference and call the correspondingcancel
method. Note: Cancelling background tasks requires cooperation from the developer.– Slaw
Nov 18 at 9:02
To add to/clarify what Slaw said - creating
Thread
s haphazardly is usually not a good idea, as you may starve the system of resources - threads aren't free, and can and should be reused. Using anExecutorService
will help you make sure you're not creating too much threads and you are reusing threads when possible.– Itai
Nov 18 at 9:18