Create a simple thread - Java Thread

Java examples for Thread:Thread

Description

Create a simple thread

Demo Code


public class SimpleMain {
  public static void main(String[] args) {
    Thread thread = new Thread(new A());
    thread.start();//from w w  w .j  a  v a  2s  .c  om
    thread.interrupt();
  }
}

class A implements Runnable {

  @Override
  public void run() {
    for (int i = 0; i < 10; i++) {
      try {
        System.out.println("Produced:" + i);
        Thread.sleep(5000);
      } catch (Exception e) {
        System.out.println(e);
      }
    }

  }

}

Related Tutorials