views:

316

answers:

4

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
+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
You don't need parentheses around new Thread(...)
Steve Kuo
+2  A: 

I would recommend reading Concurrent Programming in Java by Doug Lea.

Ascalonian
Java Concurrency in Practice is also good.
Adam Jaskiewicz
A: 

This book might be useful to you.

http://www.flazx.com/ebook3925.php

Chathuranga Chandrasekara