Saturday, 11 January 2014

Advanced Java_ Multi-threading Part 1 -- Starting Threads


==
package Demo1;

/**
 * Title:--Advanced Java_ Multi-threading Part 1 -- Starting Threads
 * 
 * @author Dharmaraj.Net This example shows how thread extend and basic work of thread
 *         and run and start method
 */

class Runner extends Thread {

 private String myString;

 public Runner(String myString) {
  this.myString = myString;
 }

 public void run() {
  for (int i = 0; i < 10; i++) {
   System.out.println(myString + "=" + i);

   try {
    Thread.sleep(100);
   } catch (InterruptedException e) {
    e.printStackTrace();
   }
  }
 }

}

public class App {

 public static void main(String[] args) {
  Runner runner1 = new Runner("Hello One");
  runner1.start();

  Runner runner2 = new Runner("Hello Two");
  runner2.start();
 }

}

No comments:

Post a Comment