0

I am new to JavaFX and multithreading in general, so based on assylias answer to this post Multithreading in JavaFX hangs the UI

i have created some Tasks running in Threads to avoid freezing the main UI thread.

The question:

I wonder what happens to the Thread and the Task when the Task has finished its job. I might also add that i am creating both the Thread and the Task in a method which is called when i press a button. So neither of them are instantiated or initialized globally.

I also wonder if there is a way to monitor threads and resource usage when compiling my code in IntelliJ.

Hope i can get some clarification on this :)

Community
  • 1
  • 1
M. Kahlen
  • 351
  • 2
  • 15
  • Assuming the thread is only running that task, it will terminate when the task's run method ends. If you don't retain references to the thread, then of course it will be eligible for garbage collection, and obviously the same is true of the task. If you are using an `Executor` (which is recommended in most cases), then the thread may be cached for reuse, depending on the `Executor` implementation you are using. – James_D Dec 06 '15 at 18:23
  • On top of what James said, you could monitor threads using VisualVM, which comes with JDK8+ – Vince Dec 06 '15 at 18:25
  • Thank you both for the quick answers. I looked a bit into the Executor Interface and I'm not quite sure what i achieve from running the Thread inside the Executor. "The thread may be cached for reuse", does this mean that i could run the same thread again but faster because its cached? Because in my current code all threads run more than once. They are, however, very light methods and I'm not sure Threading is even 'needed' but it's for learning purposes. – M. Kahlen Dec 06 '15 at 19:02
  • "Running the Thread inside the Executor": I assume you really mean "Running the Task inside the Executor". "All threads run more than once". How are you doing that? It seems to me it would be pretty complicated, because a `Task` can only be executed once. So you would need to have a `Thread` whose `run` method executed the next available task, which is really difficult to get right. The benefit of `Executor` in that case is that you don't have to write all that code. And in general the benefit of reusing a thread is that you don't have to keep creating `new Thread(...)`s, which is expensive. – James_D Dec 06 '15 at 19:09
  • Sorry, what i meant to say is: The code i run in the Thread is called several times throughout using the application but a new Thread is created each time. So I understand I want to avoid that. I took a look at the documentation for Executor and there is an example where `new Thread().start()` is called in the `execute()` method. So if I call the Task directly in the Executor will it still be assigned to another Thread than the main thread? – M. Kahlen Dec 06 '15 at 19:23
  • I think you misunderstood. The documentation you refer to is showing you how to *implement* `Executor`, and explicitly states *"The executor below spawns a new thread for each task."* You don't need to implement it; just use one of the [predefined implementations](http://docs.oracle.com/javase/8/docs/api/java/util/concurrent/Executors.html) (e.g. `Executor exec = Executors.newCachedThreadPool();`) and submit the task to it with `exec.execute(myTask)`. – James_D Dec 06 '15 at 19:30
  • Ahh i see. Thank you very much for taking your time to answer my questions. I'm not quite used to reading documentations either so pointing me in the right direction is really helpful! I will go search some more on Executors :) – M. Kahlen Dec 06 '15 at 19:42

0 Answers0