2014年9月14日 星期日

Java Multithread Examples

Java MultiTread 程式的基本結構:
import java.util.*;

class firstThread implements Runnable {
   private Thread t;
   private String threadName;

   firstThread( String name) {
      threadName = name;
      ...
   }

   public void run() {
      ...
   }

   public void start() {
      if (t==null) {
        t = new Thread (this, threadName);
        t.start();
      }
   }
} // end of class firstThread

public class first {

 public static void main(String []args) {
    try {
       ...
    } catch (Exception e) {
       System.out.println(e);
    }
 } // end of main

} // end of class first 
編輯 first.java 程式碼範例如下:
import java.util.*;

class firstThread implements Runnable {
   private Thread t;
   private String threadName;

   firstThread( String name) {
      threadName = name;
      System.out.println("Creating " + threadName);
   }

   public void run() {
      long start = System.currentTimeMillis();
      System.out.println("Running " + threadName);
      try {
         for (int i=0; i<=10; i++) {
            Random ran = new Random();
            int j = ran.nextInt(1000) + 1;
            Thread.sleep( j );
            System.out.println(" Thread " + threadName + " : round " + i + " sleep " + j);
         }
         long end = System.currentTimeMillis();
         long diff = end - start;
         System.out.println("Difference of " + threadName + " is : " + diff);
      } catch (Exception e) {
         System.out.println("Thread " + threadName + " : " + e);
      }
      System.out.println("Thread " + threadName + " exiting.");
   }

   public void start() {
      System.out.println("Starting " + threadName);
      if (t==null) {
         t = new Thread (this, threadName);
         t.start();
      }
   }
} // end of class firstThread

public class first {
   public static void main(String []args) {
      try {
         firstThread r1 = new firstThread("Thread-1");
         r1.start();

         firstThread r2 = new firstThread("Thread-2");
         r2.start();

         firstThread r3 = new firstThread("Thread-3");
         r3.start();
      } catch (Exception e) {
         System.out.println(e);
      }
   } // end of main
} // end of class first
編譯 first.java
$ javac first.java
執行 java first,因為 MultiThread 和 Random sleep 的影響,結果可能和下列範例有些不同。
$ java first

Creating Thread-1
Starting Thread-1
Creating Thread-2
Starting Thread-2
Running Thread-1
Creating Thread-3
Starting Thread-3
Running Thread-2
Running Thread-3
 Thread Thread-3 : round 0 sleep 660
 Thread Thread-2 : round 0 sleep 730
 Thread Thread-1 : round 0 sleep 837
 Thread Thread-2 : round 1 sleep 213
 Thread Thread-2 : round 2 sleep 363
 Thread Thread-1 : round 1 sleep 794
 Thread Thread-3 : round 1 sleep 995
 Thread Thread-1 : round 2 sleep 112
 Thread Thread-1 : round 3 sleep 158
 Thread Thread-2 : round 3 sleep 649
 Thread Thread-3 : round 2 sleep 578
 Thread Thread-1 : round 4 sleep 427
 Thread Thread-2 : round 4 sleep 893
 Thread Thread-2 : round 5 sleep 8
 Thread Thread-3 : round 3 sleep 625
 Thread Thread-2 : round 6 sleep 159
 Thread Thread-3 : round 4 sleep 260
 Thread Thread-1 : round 5 sleep 879
 Thread Thread-2 : round 7 sleep 667
 Thread Thread-2 : round 8 sleep 58
 Thread Thread-3 : round 5 sleep 824
 Thread Thread-2 : round 9 sleep 392
 Thread Thread-2 : round 10 sleep 2
Difference of Thread-2 is : 4146
Thread Thread-2 exiting.
 Thread Thread-1 : round 6 sleep 985
 Thread Thread-3 : round 6 sleep 667
 Thread Thread-1 : round 7 sleep 839
 Thread Thread-3 : round 7 sleep 665
 Thread Thread-1 : round 8 sleep 354
 Thread Thread-1 : round 9 sleep 96
 Thread Thread-1 : round 10 sleep 171
Difference of Thread-1 is : 5666
Thread Thread-1 exiting.
 Thread Thread-3 : round 8 sleep 559
 Thread Thread-3 : round 9 sleep 61
 Thread Thread-3 : round 10 sleep 201
Difference of Thread-3 is : 6108
Thread Thread-3 exiting.

沒有留言: