Starting a Background Task - Java Thread

Java examples for Thread:Thread Operation

Description

Starting a Background Task

Demo Code

public class Main {
  public static void main(String[] args) {
    Thread backgroundThread = new Thread(new Runnable() {
      public void run() {
        System.out.println(Thread.currentThread().getName()
            + ": is Running in the background");
      }//from   w  w  w  . j  av  a  2  s  .c  o m
    }, "Background Thread");

    System.out.println("Start");
    backgroundThread.start();
    for (int i = 0; i < 10; i++) {
      System.out.println(Thread.currentThread().getName() + ": is counting "
          + i);
    }

    System.out.println("Done");
  }
}

Result


Related Tutorials