import java
//多線程編程
public class MultiThread
{
public static void main(String args[])
{
System
//下面創建線程實例thread
ThreadUseExtends thread
//創建thread
Thread thread
thread
//thread
//優先級將決定cpu空出時
//優先級范圍
//新線程繼承創建她的父線程優先級
System
try
{
Thread
}
catch (InterruptedException e)
{
return;
}
System
if(thread
{
thread
System
}
else
System
thread
System
try
{
Thread
}
catch (InterruptedException e)
{
return;
}
System
if(thread
{
thread
System
}
else
System
System
try
{
System
}
catch (IOException e)
{
System
}
}//main
}//MultiThread
class ThreadUseExtends extends Thread
//通過繼承Thread類
//適當時候創建這一Thread子類的實例來實現多線程機制
//一個線程啟動後(也即進入就緒狀態)一旦獲得CPU將自動調用它的run()方法
{
ThreadUseExtends(){}//構造函數
public void run()
{
System
System
System
try
{
sleep(
}
catch (InterruptedException e)
{
return;
}
//如果該run()方法順序執行完了
//但如果休眠時間過長
}
}
class ThreadUseRunnable implements Runnable
//通過實現Runnable接口中的run()方法
//為參數創建Thread的線程實例
{
//Thread thread
//以這個實現了Runnable接口中run()方法的類為參數創建Thread類的線程實例
ThreadUseRunnable(){}//構造函數
public void run()
{
System
System
System
try
{
Thread
}
catch (InterruptedException e)
{
return;
}
//如果該run()方法順序執行完了
//但如果休眠時間過長
}
}
//該程序可做的修改如改休眠時間或優先級setPriority()
評論
通過繼承Thread類和實現Runnable接口的兩種方式來實現多線程編程!
From:http://tw.wingwit.com/Article/program/Java/gj/201311/27328.html