標題:
執行緒 (三)
[打印本頁]
作者:
tonyh
時間:
2019-11-23 14:48
標題:
執行緒 (三)
自訂類別,繼承Thread類別,建立執行緒。
public class Ch123 {
private MyThread th;
Ch123()
{
th=new MyThread();
th.start();
}
public static void main(String[] args) {
new Ch123();
}
}
class MyThread extends Thread
{
public void run()
{
for(int i=5; i>=1; i--)
{
System.out.println(i+"秒");
try {
sleep(1000);
} catch (InterruptedException e) {}
}
System.out.println("時間到!");
System.out.println("執行緒名稱: "+Thread.currentThread().getName());
}
}
複製代碼
public class Ch125{
MyThread th;
Ch125()
{
th=new MyThread();
th.start();
}
public static void main(String[] args) {
new Ch125();
}
class MyThread extends Thread
{
public void run()
{
for(int i=5; i>=1; i--)
{
System.out.println(i+"秒");
try {
sleep(1000);
} catch (InterruptedException e) {
System.out.println(e);
}
}
System.out.println("時間到!");
System.out.println("執行緒名稱:"+Thread.currentThread().getName());
}
}
}
複製代碼
作者:
蕭澧邦
時間:
2019-11-23 15:05
public class Ch123 {
private MyThread th;
Ch123()
{
th=new MyThread();
th.start();
}
public static void main(String[] args) {
new Ch123();
}
}
class MyThread extends Thread
{
public void run()
{
for(int i=5; i>=1; i--)
{
System.out.println(i+"秒");
try {
sleep(1000);
} catch (InterruptedException e) {}
}
System.out.println("時間到!");
System.out.println("執行緒名稱: "+Thread.currentThread().getName());
}
}
複製代碼
作者:
蔡幸融
時間:
2019-11-23 15:09
public class Ch121 extends Thread
{
MyThread th;
Ch121()
{
th=new MyThread();
th.start();
}
public static void main(String[] args)
{
new Ch121();
}
}
複製代碼
class MyThread extends Thread{
public void run() {
for(int i=5; i>=1; i--)
{
System.out.println(i+"秒");
try
{
Thread.sleep(1000);
} catch (InterruptedException e) {}
}
System.out.println("時間到!");
System.out.println("執行緒名稱: "+Thread.currentThread().getName());
}
}
複製代碼
歡迎光臨 種子論壇 | 高雄市資訊培育協會學員討論區 (http://seed.istak.org.tw/)
Powered by Discuz! 7.2