生活随笔
收集整理的這篇文章主要介紹了
线程应用
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
基本概念
進程是具有一定獨立功能的程序關于某個數據集合上的一次運行活動,進程是系統進行資源分配和調度的一個獨立單位
線程是進程的一個實體,是cpu調度和分派的基本單位,它是比進程更小的能獨立運行的基本單位,線程自己基本不擁有系統資源,只擁有一點在運行是必不可少的資源(如程序計數器,一組寄存器和棧)
線程屬性
線程總是屬于下列四個狀態的某一個狀態中
- new (新建狀態)
- runnable(可運行狀態)
- blocked(阻塞/掛起狀態)
- 終止狀態
一個處于runnable狀態的線程并不一定在運行,這取決于操作系統是否給這個線程分配時間來運行
盜來的狀態圖《Java 2核心技術卷2:高級特性》
實現線程的2種方式
繼承Thread和實現Runnable接口
繼承Thread
public class MyThreadTest {public static void main(String args[]) {MyThread myThread = new MyThread();myThread.start();}
}class MyThread extends Thread {@Overridepublic void run() {for(int i = 0; i < 10; i++) {System.out.println("the Thread is running!");try {Thread.sleep(1000);} catch (InterruptedException e) {e.printStackTrace();}}}
}
實現Runnable接口
public class MyThreadTest1 {public static void main(String args[]) {MyThread1 myThread = new MyThread1();Thread thread = new Thread(myThread);thread.start();}
}class MyThread1 implements Runnable {@Overridepublic void run() {for(int i = 0; i < 10; i++) {System.out.println("the Thread is running!");try {Thread.sleep(1000);} catch (InterruptedException e) {e.printStackTrace();}}}
}
同步
在大多數應用多線程應用程序中,兩個或者兩個以上的線程需要共享相同的對象,假設2個線程共享同一對象,而且每一個線程都調用了能改變它狀態的方法,他們會互相破壞對方的執行。
比如一個方法public void add() {xxxx;xxxx;xxxx}方法體中有3個步驟,為了防止add方法執行到中間步驟的時候被中斷,可以為add方法加鎖 ?
public synchronized void add(){xxx;xxx;xxxx;}
wait()和notify()方法
wait()阻塞當前線程;notify()隨機的在等待隊列中刪除一個線程,notifyAll()方法刪除一個等待隊列的所有線程,當線程離開等待隊列后,線程調度程序會再次喚醒它們,被喚醒后它們將試圖重新進入這個對象。一旦這個對象解鎖,被喚醒線程中的一個將進入這個對象,并繼續執行wait()方法以后的語句
消費與生產的問題
public class ProducerConsumerTest {public static void main(String args[]) {ProductStorage productStorage = new ProductStorage();Producers producer = new Producers(productStorage);Consumers consumer = new Consumers(productStorage);Thread producerThread = new Thread(producer);Thread consumerThread = new Thread(consumer);producerThread.start();consumerThread.start();}
}/*** 存儲箱,由數組結構組成, 數組中元素為產品production*/
class ProductStorage {private int index = 0;// 數組下表,產品位置,默認初始位置為0,表示當前為空private Production[] productions = new Production[5];// 設定存儲箱大小為5public synchronized void push(Production p) {if (index == productions.length) {// wait();} catch (InterruptedException e) {e.printStackTrace();}}notifyAll();// 喚醒所有等待的生產線程productions[index] = p;System.out.println("生產者放入 " + index + "位置---" + p);index++;//浮標向上移動}public synchronized void pop() {if (index == 0) {// 參考棧,如果數組已經空了,調用wait方法,當前消費線程進入阻塞狀態try {wait();} catch (InterruptedException e) {e.printStackTrace();}}index--;//浮標向下移動notifyAll();// 喚醒所有等待的消費線程System.out.println("消費者從" + index + "位置取出" + productions[index]);}
}class Production {private int id;public Production(int id) {this.id = id;}public String toString() {return "產品" + id;}
}class Consumers implements Runnable {private ProductStorage productStorage = null;public Consumers(ProductStorage productStorage) {this.productStorage = productStorage;}@Overridepublic void run() {for (int i = 0; i < 20; i++) {productStorage.pop();try {Thread.sleep(2000);} catch (InterruptedException e) {e.printStackTrace();}}}}class Producers implements Runnable {private ProductStorage productStorage = null;public Producers(ProductStorage productStorage) {this.productStorage = productStorage;}@Overridepublic void run() {for (int i = 0; i < 10; i++) {Production p = new Production(i);productStorage.push(p);try {Thread.sleep(1000);} catch (InterruptedException e) {e.printStackTrace();}}}}
幾篇關于線程的博客http://www.cnblogs.com/rollenholt/archive/2011/08/28/2156357.html
http://www.cnblogs.com/bingoidea/archive/2011/03/29/1998923.html
轉載于:https://my.oschina.net/liangzhenghui/blog/110111
總結
以上是生活随笔為你收集整理的线程应用的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。