Lukap
Lukap

Reputation: 31963

Two threads in one Java applet best practice

I have seen many examples like this:

public class AppletName extends Applet implements Runnable {
    // member variables
    // run method and so on...
}

The problem that I cannot figured out is what is the best way to use two threads in one applet. I tried to find this on Google I have found books with 500 pages :(, I need something quick.

Can someone give me some short example or tell me what is the best practice?

Upvotes: 3

Views: 3775

Answers (4)

Araejay
Araejay

Reputation: 222

It takes a while to perfect Applet and Java GUI apps that are very responsive.

You must understand that its ok to extend a thread, but what you do with it is important.

There are many frameworks to utilize, if you need something fast, then extending a thread is fine.

You may need to ask yourself from a design perspective:

  1. Does you app have a multiple pages / frames?

  2. Does it updates often? (excessive paintings, or just many progress bars)

  3. Does it require user input often (eg game controls)

If you answer Yes to all, then consider wrapping to a thread, its a good practice to start the main process (background) thread before the display thread.

again, I have no idea what you would like to achieve, perhaps enlightening us.

Upvotes: 0

Andrew Thompson
Andrew Thompson

Reputation: 168825

I have seen many examples like this

If they are based on Applet, they are either very old or written by relatively incompetent authors. Put them back where you found them.

  1. Use Swing for the components (e.g. extend JApplet).
  2. Read Concurrency in Swing for more details of how to use multiple threads.

Upvotes: 1

andrey
andrey

Reputation: 842

An example of Java Applets and Threads. Short and simple. With example code.

If you have a pool of tasks for concurrent processing consider ExecutorService class.

For best practices there is a more short (<500 pages :) and the most definitive guides is here. Chapters 07 - 11 related to multithreaded code. (just a list of best practices and typical mistakes)

Upvotes: 0

Peter Lawrey
Peter Lawrey

Reputation: 533500

I would use an ExecutorService or a ScheduledExecutorService and add tasks e.g. anonymous Runnable classes to the thread pool. You may find you only need one thread even though you have two or most tasks (its just a configuration of the thread pool which you can change later)

Upvotes: 0

Related Questions