Java Tutorial/Thread/Thread Join

Материал из Java эксперт
Версия от 15:18, 31 мая 2010; Admin (обсуждение | вклад) (1 версия)
(разн.) ← Предыдущая | Текущая версия (разн.) | Следующая → (разн.)
Перейти к: навигация, поиск

Specify the number of milliseconds to wait for the death of a thread

class TryThread extends Thread {
  public TryThread(String firstName, String secondName, long delay) {
    this.firstName = firstName;
    this.secondName = secondName;
    aWhile = delay;
    setDaemon(true);
  }
  public void run() {
    try {
      while (total < 1000) {
        System.out.print(firstName);
        sleep(aWhile);
        total += aWhile;
        System.out.print(secondName + "\n");
      }
      System.out.print(secondName + " stoped.\n");
    } catch (InterruptedException e) {
      System.out.println(firstName + secondName + e);
    }
  }
  private String firstName;
  private String secondName;
  private long aWhile;
  private long total;
}
public class MainClass {
  public static void main(String[] args) {
    Thread first = new TryThread("A ", "a ", 200L);
    Thread second = new TryThread("B ", "b ", 300L);
    Thread third = new TryThread("C ", "c ", 500L);
    first.start();
    second.start();
    third.start();
    try {
      first.join(2000); // Wait up to 2 second for thread1 to die
    } catch (InterruptedException e) {
      e.printStackTrace();
    }
  }
}



B A C a 
A b 
B a 
A c 
C a 
A b 
B a 
A b 
B c 
c  stoped.
a 
a  stoped.


Thread Join Demo

Thread Joinclass TryThread extends Thread {
  public TryThread(String firstName, String secondName, long delay) {
    this.firstName = firstName;
    this.secondName = secondName;
    aWhile = delay;
    setDaemon(true);
  }
  public void run() {
    try {
      while (total < 1000) {
        System.out.print(firstName);
        sleep(aWhile);
        total += aWhile;
        System.out.print(secondName + "\n");
      }
      System.out.print(secondName + " stoped.\n");
    } catch (InterruptedException e) {
      System.out.println(firstName + secondName + e);
    }
  }
  private String firstName;
  private String secondName;
  private long aWhile;
  private long total;
}
public class MainClass {
  public static void main(String[] args) {
    Thread first = new TryThread("A ", "a ", 200L);
    Thread second = new TryThread("B ", "b ", 300L);
    Thread third = new TryThread("C ", "c ", 500L);
    first.start();
    second.start();
    third.start();
    try {
      first.join();
    } catch (InterruptedException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }
  }
}



A B C a 
A b 
B a 
A c 
C a 
A b 
B a 
A b 
B c 
c  stoped.
a 
a  stoped.


Using join() to wait for threads to finish.

class NewThread implements Runnable {
  String name; // name of thread
  Thread t;
  NewThread(String threadname) {
    name = threadname;
    t = new Thread(this, name);
    System.out.println("New thread: " + t);
    t.start(); // Start the thread
  }
  public void run() {
    try {
      for (int i = 5; i > 0; i--) {
        System.out.println(name + ": " + i);
        Thread.sleep(1000);
      }
    } catch (InterruptedException e) {
      System.out.println(name + " interrupted.");
    }
    System.out.println(name + " exiting.");
  }
}
class DemoJoin {
  public static void main(String args[]) {
    NewThread ob1 = new NewThread("One");
    NewThread ob2 = new NewThread("Two");
    NewThread ob3 = new NewThread("Three");
    System.out.println("Thread One is alive: " + ob1.t.isAlive());
    System.out.println("Thread Two is alive: " + ob2.t.isAlive());
    System.out.println("Thread Three is alive: " + ob3.t.isAlive());
    try {
      System.out.println("Waiting for threads to finish.");
      ob1.t.join();
      ob2.t.join();
      ob3.t.join();
    } catch (InterruptedException e) {
      System.out.println("Main thread Interrupted");
    }
    System.out.println("Thread One is alive: " + ob1.t.isAlive());
    System.out.println("Thread Two is alive: " + ob2.t.isAlive());
    System.out.println("Thread Three is alive: " + ob3.t.isAlive());
    System.out.println("Main thread exiting.");
  }
}





Wait for the threads to finish

public class Main {
  public static void main(String[] args) throws Exception {
    Thread thread1 = new Thread(new TestThread(1));
    Thread thread2 = new Thread(new TestThread(2));
    thread1.start();
    thread2.start();
    thread1.join();
    thread2.join();
  }
}
class TestThread implements Runnable {
  int id;
  public TestThread(int id) {
    this.id = id;
  }
  public void run() {
    for (int i = 1; i <= 10; i++) {
      System.out.println("Thread" + id + ": " + i);
    }
  }
}





Wait the for the completion of a thread

public class Main {
  public static void main(String a[]) throws Exception {
    MyThread tt1 = new MyThread(50);
    MyThread tt2 = new MyThread(75);
    Thread t1 = new Thread(tt1, "Test thread 1");
    Thread t2 = new Thread(tt2, "Test thread 2");
    t1.start();
    t2.start();
    t1.join();
    t2.join();
  }
}
class MyThread implements Runnable {
  int i;
  MyThread(int i) {
    super();
    this.i = i;
  }
  public void run() {
     System.out.println(Thread.currentThread().getName() + " " + i);
  }
}