Reputation: 31963
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
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:
Does you app have a multiple pages / frames?
Does it updates often? (excessive paintings, or just many progress bars)
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
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.
JApplet
). Upvotes: 1
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
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