View Single Post
  #2   Spotlight this post!  
Unread 01-05-2012, 17:44
vinnie's Avatar
vinnie vinnie is offline
Registered User
FRC #3309 (Friarbots)
Team Role: College Student
 
Join Date: Oct 2010
Rookie Year: 2010
Location: Los Angeles / Anaheim
Posts: 107
vinnie has a reputation beyond reputevinnie has a reputation beyond reputevinnie has a reputation beyond reputevinnie has a reputation beyond reputevinnie has a reputation beyond reputevinnie has a reputation beyond reputevinnie has a reputation beyond reputevinnie has a reputation beyond reputevinnie has a reputation beyond reputevinnie has a reputation beyond reputevinnie has a reputation beyond repute
Re: Multitasking in Java

The argument to the Thread constructor takes a Runnable. To start the Thread use the start() method, not run() (If you call run(), it won't be threaded at all).

I usually write a separate class that implements Runnable, then creates and starts a thread in the constructor. Take this simple class for an example:

Code:
public class ThreadedHello implements Runnable{
    public ThreadedHello(){
        new Thread(this).start();
    }

    public void run(){
        System.out.println("Hello World!");
    }
}
Then, to use this class I would just do this in another class:

Code:
ThreadedHello hello = new ThreadedHello();
//if it were a real object I would do something else besides create it
Reply With Quote