標題:
執行緒 (四)
[打印本頁]
作者:
tonyh
時間:
2016-10-1 17:46
標題:
執行緒 (四)
本帖最後由 tonyh 於 2016-10-15 15:57 編輯
自訂多個類別,繼承Thread類別,建立執行緒。
[attach]1782[/attach]
public class Ch124 {
private MyThread1 th1;
private MyThread2 th2;
Ch124()
{
th1=new MyThread1();
th1.start();
th2=new MyThread2();
th2.start();
}
public static void main(String[] args) {
new Ch124();
}
}
class MyThread1 extends Thread
{
public void run()
{
for(int i=5; i>=1; i--)
{
System.out.println(i+"秒 "+Thread.currentThread().getName());
try {
sleep(1000);
} catch (InterruptedException e) {}
}
System.out.println("時間到! "+Thread.currentThread().getName());
}
}
class MyThread2 extends Thread
{
public void run()
{
for(int i=1; i<=5; i++)
{
System.out.println(i+"秒 "+Thread.currentThread().getName());
try {
sleep(1000);
} catch (InterruptedException e) {}
}
System.out.println("時間到! "+Thread.currentThread().getName());
}
}
複製代碼
作者:
李允軒
時間:
2016-10-1 17:57
public class Ch123 {
private MyThread th;
private MyThread1 th1;
Ch123(){
th=new MyThread();
th.start();
th1=new MyThread1();
th1.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+"秒"+Thread.currentThread().getName());
try{
Thread.sleep(1000);
}catch(InterruptedException e){}
}
System.out.println("時間到");
System.out.println("執行緒名稱: "+ Thread.currentThread().getName());
}
}
class MyThread1 extends Thread{
public void run(){
for(int i=5; i>=1; i--){
System.out.println(i+"秒"+Thread.currentThread().getName());
try{
Thread.sleep(1000);
}catch(InterruptedException e){}
}
System.out.println("時間到");
System.out.println("執行緒名稱: "+ Thread.currentThread().getName());
}
}
複製代碼
作者:
劉得恩
時間:
2016-10-1 17:58
public class Ch124
{
private MyThread1 th1;
private MyThread2 th2;
Ch124()
{
th1=new MyThread1();
th1.start();
th2=new MyThread2();
th2.start();
}
public static void main(String args[])
{
new Ch124();
}
}
class MyThread1 extends Thread
{
public void run(){
for(int i=5;i>=1;i--)
{
System.out.println(i+"秒 "+Thread.currentThread().getName());
try
{
sleep(1000);
}catch(InterruptedException e){}
}
System.out.println("時間到");
System.out.println("執行緒名稱"+Thread.currentThread().getName());
}
}
class MyThread2 extends Thread
{
public void run(){
for(int i=5;i>=1;i--)
{
System.out.println(i+"秒 "+Thread.currentThread().getName());
try
{
sleep(1000);
}catch(InterruptedException e){}
}
System.out.println("時間到");
System.out.println("執行緒名稱"+Thread.currentThread().getName());
}
}
複製代碼
作者:
張峻瑋
時間:
2016-10-1 17:58
public class Ch124 {
private MyThread1 th1;
private MyThread2 th2;
Ch124()
{
th1=new MyThread1();
th1.start();
th2=new MyThread2();
th2.start();
}
public static void main(String[] args) {
new Ch124();
}
}
class MyThread1 extends Thread
{
public void run()
{
for(int i=5;i>=1;i--)
{
System.out.println(i+"秒"+Thread.currentThread().getName());
try {
Thread.sleep(1000);
} catch (InterruptedException e) {}
}
System.out.println("時間到");
System.out.println("執行緒名稱:"+Thread.currentThread().getName());
}
}
class MyThread2 extends Thread
{
public void run()
{
for(int i=5;i>=1;i--)
{
System.out.println(i+"秒"+Thread.currentThread().getName());
try {
Thread.sleep(1000);
} catch (InterruptedException e) {}
}
System.out.println("時間到");
System.out.println("執行緒名稱:"+Thread.currentThread().getName());
}
}
複製代碼
作者:
張彥承
時間:
2016-10-1 18:00
public class Ch100
{
private MyThread1 th1;
private MyThread2 th2;
Ch100()
{
th1=new MyThread1();
th1.start();
th2=new MyThread2();
th2.start();
}
public static void main(String[] args)
{
new Ch100();
}
}
class MyThread1 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());
}
}
class MyThread2 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());
}
}
複製代碼
作者:
林宇翔
時間:
2016-10-1 18:03
public class Ch121 {
private MyThread1 th1;
private MyThread2 th2;
Ch121()
{
th1=new MyThread1();
th1.start();
th2=new MyThread2();
th2.start();
}
public static void main(String[] args) {
new Ch121();
}
}
class MyThread1 extends Thread
{
public void run()
{
for(int i=5; i>=1; i--)
{
System.out.println(i+"秒"+Thread.currentThread().getName());
try {
Thread.sleep(1000);
} catch (InterruptedException e) {}
}
System.out.println("時間到!");
System.out.println("執行緒名稱: "+Thread.currentThread().getName());
}
}
class MyThread2 extends Thread
{
public void run()
{
for(int i=5; i>=1; i--)
{
System.out.println(i+"秒"+Thread.currentThread().getName());
try {
Thread.sleep(1000);
} catch (InterruptedException e) {}
}
System.out.println("時間到!");
System.out.println("執行緒名稱: "+Thread.currentThread().getName());
}
}
複製代碼
歡迎光臨 種子論壇 | 高雄市資訊培育協會學員討論區 (http://istak.org.tw/seed/)
Powered by Discuz! 7.2