1.線程
(1)理解,線程是系統分配處理器時間資源的基本單元也是系統調用的基本單位,簡單理解就是一個或多個線程組成了一個進程,進程就像爸爸,線程就像兒子,有時候爸爸一個人干不了活就生了幾個兒子干活,會比較快,例如你打開視頻軟件在線看視頻,那有一個線程負責下載,一個線程負責播放...,視頻軟件就相當爸爸,線程就像兒子們,好處就是可以並發,效率高;
(2)線程一般有兩種方法,很簡單 下面例子懶得寫以前有參照過別人的 http://www.myexception.cn/program/1056162.html
2-1.繼承Thread線程類
class ThreadUseExtends extends Thread
//通過繼承Thread類,並實現它的抽象方法run()
//適當時候創建這一Thread子類的實例來實現多線程機制
//一個線程啟動后(也即進入就緒狀態)一旦獲得CPU將自動調用它的run()方法
{
ThreadUseExtends(){}//構造函數
public void run()
{
System.out.println("我是Thread子類的線程實例!");
System.out.println("我將掛起10秒!");
System.out.println("回到主線程,請稍等,剛才主線程掛起可能還沒醒過來!");
try
{
sleep(10000);//掛起10秒
}
catch (InterruptedException e)
{
return;
}
//如果該run()方法順序執行完了,線程將自動結束,而不會被主線程殺掉
//但如果休眠時間過長,則線程還存活,可能被stop()殺掉
}
}
2-2.實現 Runnable接口 代碼簡單
class ThreadUseRunnable implements Runnable
//通過實現Runnable接口中的run()方法,再以這個實現了run()方法的類
//為參數創建Thread的線程實例
{
//Thread thread2=new Thread(this);
//以這個實現了Runnable接口中run()方法的類為參數創建Thread類的線程實例
ThreadUseRunnable(){}//構造函數
public void run()
{
System.out.println("我是Thread類的線程實例並以實現了Runnable接口的類為參數!");
System.out.println("我將掛起1秒!");
System.out.println("回到主線程,請稍等,剛才主線程掛起可能還沒醒過來!");
try
{
Thread.sleep(1000);//掛起1秒
}
catch (InterruptedException e)
{
return;
}
//如果該run()方法順序執行完了,線程將自動結束,而不會被主線程殺掉
//但如果休眠時間過長,則線程還存活,可能被stop()殺掉
}
}
2-3.接下來測試以上兩個類
public class ThreadTest {
/**
*學習多線程例子
* @param args
* @author JJ
*/
public static void main(String[] args) {
System.out.println("我是主線程!");
//下面創建線程實例thread1
ThreadUseExtends thread1=new ThreadUseExtends();
//創建thread2時以實現了Runnable接口的THhreadUseRunnable類實例為參數
Thread thread2=new Thread(new ThreadUseRunnable(),"SecondThread");
thread1.start();//啟動線程thread1使之處於就緒狀態
//thread1.setPriority(6);//設置thread1的優先級為6
//優先級將決定cpu空出時,處於就緒狀態的線程誰先占領cpu開始運行
//優先級范圍1到10,MIN_PRIORITY,MAX_PRIORITY,NORM_PAIORITY
//新線程繼承創建她的父線程優先級,父線程通常有普通優先級即5NORM_PRIORITY
System.out.println("主線程將掛起7秒!");
try
{
Thread.sleep(3000);//主線程掛起7秒
}
catch (InterruptedException e)
{
return;
}
System.out.println("又回到了主線程1!");
if(thread1.isAlive())
{
thread1.stop();//如果thread1還存在則殺掉他
System.out.println("thread1休眠過長,主線程殺掉了thread1!");
}
else
System.out.println("主線程沒發現thread1,thread1已醒順序執行結束了!");
thread2.start();//啟動thread2
System.out.println("主線程又將掛起7秒!");
try
{
Thread.sleep(7000);//主線程掛起7秒
}
catch (InterruptedException e)
{
return;
}
System.out.println("又回到了主線程2!");
if(thread2.isAlive())
{
thread2.stop();//如果thread2還存在則殺掉他
System.out.println("thread2休眠過長,主線程殺掉了thread2!");
}
else
System.out.println("主線程沒發現thread2,thread2已醒順序執行結束了!");
System.out.println("程序結束按任意鍵繼續!");
try
{
System.in.read();
}
catch (IOException e)
{
System.out.println(e.toString());
}
}//main
}
2-4.運行查看效果

2.線程池
(1)理解,線程池就是用來管理線程的,線程要創建摧毀很耗時和內存資源,就像一次性筷子生產到扔掉,我們線程池就像家用筷子,可以用很多次,線程池就是這么一個作用
(2)例子,我覺得馮小衛寫了很好我參考了一下他的 http://blog.csdn.net/coding_or_coded/article/details/6856014,線程池有好幾種,各有利弊
2-1.固定大小的線程池,newFixedThreadPool:
- package app.executors;
- import java.util.concurrent.Executors;
- import java.util.concurrent.ExecutorService;
- /**
- * Java線程:線程池
- *
- * @author 馮小衛
- */
- public class Test {
- public static void main(String[] args) {
- // 創建一個可重用固定線程數的線程池
- ExecutorService pool = Executors.newFixedThreadPool(5);
- // 創建線程
- Thread t1 = new MyThread();
- Thread t2 = new MyThread();
- Thread t3 = new MyThread();
- Thread t4 = new MyThread();
- Thread t5 = new MyThread();
- // 將線程放入池中進行執行
- pool.execute(t1);
- pool.execute(t2);
- pool.execute(t3);
- pool.execute(t4);
- pool.execute(t5);
- // 關閉線程池
- pool.shutdown();
- }
- }
- class MyThread extends Thread {
- @Override
- public void run() {
- System.out.println(Thread.currentThread().getName() + "正在執行。。。");
- }
- }
運行結果和其他解釋具體可以參照一下上面鏈接
