?? morethreads.java
字號:
//創造多線程
class MyThread implements Runnable{
int count;
Thread thrd;
//創造一個新的線程/
MyThread(String name){
thrd = new Thread(this, name);
count = 0;
thrd.start();//start the thread.
}
//beginning execution of new thread.
public void run(){
System.out.println(thrd.getName()+" 線程開始.");
try{
do{
Thread.sleep(500);
System.out.println("在 "+ thrd.getName()+
", 計數到 "+count);
count++;
}while(count<5);
}catch(InterruptedException e){
System.out.println(thrd.getName()+" 被打斷.");
}
System.out.println(thrd.getName()+ " 結束.");
}
}
public class MoreThreads {
public static void main(String args[]){
System.out.println("主線程開始:");
MyThread mt1= new MyThread("線程 1");
MyThread mt2 = new MyThread("線程 2");
MyThread mt3 = new MyThread("線程 3");
do{
System.out.print(".");
try{
Thread.sleep(100);
}catch(InterruptedException e){
System.out.println("主線程被打斷.");
}
}while((mt1.count<5)&&(mt2.count<5)&&(mt3.count<5));
System.out.println("主線程結束.");
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -