Run more than 2 threads inside a class

advertisements

I've got a class which starts a new thread by calling

public void omnom(){
   t = new Thread(this, "My Thread");
   t.start();
}

The new thread then runs inside the run() method. So I have two threads working fine but how would I add a third? I'd like to "split" off 2 threads e.g t.start(); + f.start(); how would I split off both of them from omnom() and send them to 2 different "run()" methods?

Thanks!


You could create an anonymous Runnable class to call whatever "run" method you prefer:

public void omnom() {
    new Thread(this, "My Thread").start();
    new Thread(
            new Runnable() {
                public void run() {
                    otherRunMethod();
                }
            }
        ).start();
}