Java 線程







package 線程;
//先繼承Thread
public class SimpleThread extends Thread{


public SimpleThread(String name ){//參數爲線程的名稱
setName(name);
}
public void run(){//覆蓋run()方法
int i = 0 ;
while(i++ < 5 )//循環5 詞
try {
System.out.println(getName()+"執行步驟"+i);
Thread.sleep(1000);//休眠1秒
} catch (Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
SimpleThread thread1 = new SimpleThread("線程1");
SimpleThread thread2 = new SimpleThread("線程2");
thread1.start();//啓動線程1
thread2.start();//啓動線程2
}


}



。>>>>>>>>>>>>>>>>>>>





package 線程;
//繼承實現Runnable接口的方法
public class SimpleRunnable implements Runnable {


public void run(){//覆蓋run方法
int i = 15 ;
while(i-- > 1){//循環5 次
try {
System.out.println("#");
Thread.sleep(500);
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
}
}
public static void main(String[] args) {
Thread thread1 = new Thread(new SimpleRunnable(),"線程1");
thread1.start();//啓動線程1


}


}

>>>>>>>>>>






》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》



》》》》》》》》》》》》》》》》》》》》

線程的控制



》》》》》》》》》》》》》》》







》》》》》




》》》》》》》》》》》











發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章