How do I create a new thread and have it start running?

John Zukowski

Creating a thread involves creating a new Thread and invoking its start() method. Calling start() causes the run() method of the Thread subclass or the Runnable object passed to the Thread constructor to execute.


Thread t1 = new Thread() {
  public void run() {
    for (int i=0; i<100; i++) {
      System.out.println("Tastes Great");
    }
  }
};

Runnable r = new Runnable() {
  public void run() {
    for (int i=0; i<100; i++) {
      System.out.println("Less Filling");
    }
  }
};
Thread t2 = new Thread(r);

t1.start();
t2.start();
0 Comments  (click to add your comment)
Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

About | Sitemap | Contact