標題:
執行緒 (四) - 多執行緒
[打印本頁]
作者:
tonyh
時間:
2019-11-23 14:49
標題:
執行緒 (四) - 多執行緒
自訂多個類別,繼承Thread類別,建立執行緒。
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());
}
}
複製代碼
作者:
蔡幸融
時間:
2019-11-23 15:11
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());
}
}
複製代碼
作者:
蕭澧邦
時間:
2019-11-30 14:21
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());
}
}
複製代碼
作者:
蔡幸融
時間:
2019-11-30 14:26
public class Ch125 {
void show1()
{
Thread th=new Thread("show1")
{
public void run()
{
for(int i=5; i>=1; i--)
{
System.out.println(i+"秒 "+currentThread().getName());
try {
sleep(1000);
} catch (InterruptedException e) {}
}
System.out.println("時間到! "+Thread.currentThread().getName());
}
};
th.start();
}
void show2()
{
Thread th=new Thread("show2")
{
public void run()
{
for(int i=5; i>=1; i--)
{
System.out.println(i+"秒 "+currentThread().getName());
try {
sleep(1000);
} catch (InterruptedException e) {}
}
System.out.println("時間到! "+Thread.currentThread().getName());
}
};
th.start();
}
Ch125()
{
show1();
show2();
}
public static void main(String[] args) {
new Ch125();
}
}
複製代碼
作者:
莊旻叡
時間:
2019-12-26 20:19
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());
}
}
複製代碼
歡迎光臨 種子論壇 | 高雄市資訊培育協會學員討論區 (http://istak.org.tw/seed/)
Powered by Discuz! 7.2