How do I implement multiple threads in Java?
+12
A:
Start here: Java Sun Tutorial, Lesson: Concurrency
And perhaps focus on this part: High Level Concurrency Objects
Nicolai
2009-03-17 12:01:40
+3
A:
You could do it this way:
Write a class that implements Runnable interface and put the thread code in the run() method.
public class MyThreadedClass implements Runnable {
public void run()
{
// put your thread code here
}
}
And then, create the thread anywhere you want like this.
(new Thread(new MyThreadedClass())).start();
That's a very simple cookbook.
Don't forget to read about synchronization and concurrency as other post is suggesting. It's very important when dealing with threads.
Pablo Santa Cruz
2009-03-17 12:06:36
You don't need parentheses around new Thread(...)
Steve Kuo
2009-03-17 20:02:03
+2
A:
I would recommend reading Concurrent Programming in Java by Doug Lea.
Ascalonian
2009-03-17 14:45:03