0

I'm trying to learn how to thread a class (specifically a method in a class) and I've come across Thread() and SwingWorker(). This is a swing application. Which should I choose and why? Also, is there some generic way to add implementation for threading to existing methods?

Thanks

1
  • 3
    What does the javadoc of these two classes say? What do you conclude? Commented Mar 12, 2012 at 17:46

3 Answers 3

1

SwingWorker is mainly for executing background processes in Java Swing means UI based application , like on pressing a Button in a UI , you want some long process to happen in Background. Thread is normally used to multitasking in Java Programs like executing two operations in a time kind of stuffs. Thread can be implemented from Runnable interface as well as inherited from thread Class. Check Oracle Java Docs.

Sign up to request clarification or add additional context in comments.

Comments

0

Using Swingworkers would probably make your like easier because it is meant to do exactly what you need. There is a good tutorial on Oracle's website that would get you started. In essence, in a Swing application, you need to make sure that:

  • Anything that interacts with the GUI runs in the EDT (Event Dispatch Thread)
  • Long tasks do not run on the EDT because if they do they will freeze the GUI while your computation is running

Swingworkers handle those 2 things very well.

Comments

0

Class Thread is a basic piece you need to create threads. JDK provides either "low level" API (take a look on class Thread, interface Runnable, synchoronized keyword, methods wait(), notify()) or higher level API (SwingWorker, Timer, executors framework).

There is a lot of tutorials in web. Learn basics first. Read javadoc of Thread, find some examples, play with them. Then go through Timer and SwingWorker. It will be simple. Then, when you understand how is it working take your time to study executors, thread pools etc.

Happy threading!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.