1.實現(xiàn)Runnable接口
package hasreturn;

public class MyThread implements Runnable
{
private String name;
public MyThread(String name)
{
this.name = name;
}
public void run()
{
try
{
Thread.sleep(1000*3);
}
catch(Exception ex)
{}
System.out.println(this.name + "正在執(zhí)行
..");
}
}
2. 線程池
package hasreturn;

import java.util.concurrent.Executors;

import java.util.concurrent.ExecutorService;

public class ThreadPool
{
public static void main(String[] args)
{
// 創(chuàng)建一個可重用固定線程數(shù)的線程池
ExecutorService pool = Executors.newFixedThreadPool(1);
// 創(chuàng)建實現(xiàn)了Runnable接口對象,Thread對象當(dāng)然也實現(xiàn)了Runnable接口
MyThread t1 = new MyThread("A");
MyThread t2 = new MyThread("B");
MyThread t3 = new MyThread("C");
MyThread t4 = new MyThread("D");
MyThread t5 = new MyThread("E");
// 將線程放入池中進(jìn)行執(zhí)行
pool.execute(t1);
pool.execute(t2);
pool.execute(t3);
pool.execute(t4);
pool.execute(t5);
// 關(guān)閉線程池
pool.shutdown();
}
}



























































