2013年6月4日 星期二

執行序Thread實例

題目使用多執行序一起執行在執行的時候印出現在時間,利用不同的開頭字串區別開來,只利用for迴圈i的記數來緩衝無限迴圈,當把記數次數調低而出現次數會變多。
import java.util.Date;

class TimerThread extends Thread{
 String s;
 TimerThread(String ss){
  s=ss;

 }

 public void run(){
  while(true){
   for(int i=0;i<1000000000;i++);
   Date now=new Date();
   System.out.println(s+" thread >>>" + now);
  }
 }
}

public class ex0604a2{
    public static void main(String[] argv){
 TimerThread Thread1=new TimerThread("+++++");
 Thread1.start();
 TimerThread Thread2=new TimerThread("-----");
 Thread2.start();
 TimerThread Thread3=new TimerThread("******");
 Thread3.start();
     
  while (true) {
   for(int i=0;i<500000000;i++);
   Date now=new Date();
   System.out.println(" main >>>" + now);
  }

 }
}

沒有留言:

張貼留言