Java多線(xiàn)程的相關(guān)機(jī)制
一 線(xiàn)程的基本概念
線(xiàn)程是一個(gè)程序內(nèi)部的順序控制流.一個(gè)進(jìn)程相當(dāng)于一個(gè)任務(wù),一個(gè)線(xiàn)程相當(dāng)于一個(gè)任務(wù)中的一條執(zhí)行路徑.;多進(jìn)程:在操作系統(tǒng)中能同時(shí)運(yùn)行多個(gè)任務(wù)(程序);多線(xiàn)程:在同一個(gè)應(yīng)用程序中有多個(gè)順序流同時(shí)執(zhí)行;Java的線(xiàn)程是通過(guò)java.lang.Thread類(lèi)來(lái)實(shí)現(xiàn)的;JVM啟動(dòng)時(shí)會(huì)有一個(gè)由主方法(public static void main(){})所定義的線(xiàn)程;可以通過(guò)創(chuàng)建Thread的實(shí)例來(lái)創(chuàng)建新的線(xiàn)程;每個(gè)線(xiàn)程都是通過(guò)某個(gè)特定Thread對(duì)象所對(duì)應(yīng)的方法run()來(lái)完成其操作的,方法run()稱(chēng)為線(xiàn)程體,通過(guò)調(diào)用Thread類(lèi)的start()方法來(lái)啟動(dòng)一個(gè)線(xiàn)程。
二 線(xiàn)程的創(chuàng)建和啟動(dòng)
可以有兩種方式創(chuàng)建新的線(xiàn)程:
***種:
1.定義線(xiàn)程類(lèi)實(shí)現(xiàn)Runnable接口
2.Thread myThread = new Thread(target); //target為Runnable接口類(lèi)型
3.Runnable中只有一個(gè)方法:public void run();用以定義線(xiàn)程運(yùn)行體
4.使用Runnable接口可以為多個(gè)線(xiàn)程提供共享的數(shù)據(jù)
5.在實(shí)現(xiàn)Runnable接口的類(lèi)的run()方法定義中可以使用Thread的靜態(tài)方法public static Thread currentThread();獲取當(dāng)前線(xiàn)程的引用
第二種:
1.可以定義一個(gè)Thread的子類(lèi)并重寫(xiě)其run方法如:
class MyThread extends Thread {
public void run() {...}
}
2.然后生成該類(lèi)的對(duì)象:
MyThread myThread = new MyThread();
三 線(xiàn)程控制的基本方法
isAlive():判斷線(xiàn)程是否還"活"著
getPriority():獲得線(xiàn)程的優(yōu)先級(jí)數(shù)值
setPriority():設(shè)置線(xiàn)程的優(yōu)先級(jí)數(shù)值
Thread.sleep():將當(dāng)前線(xiàn)程睡眠指定毫秒數(shù)
join():調(diào)用某線(xiàn)程的該方法,將當(dāng)前線(xiàn)程與該線(xiàn)程"合并",即等待該線(xiàn)程結(jié)束,再恢復(fù)當(dāng)前線(xiàn)程的運(yùn)行
yield():讓出cpu,當(dāng)前線(xiàn)程進(jìn)入就緒隊(duì)列等待調(diào)度
wait():當(dāng)前線(xiàn)程進(jìn)入對(duì)象的wait pool
notify()/notifyAll():喚醒對(duì)象的wait pool中的一個(gè)/所有等待線(xiàn)程
四 線(xiàn)程同步
實(shí)現(xiàn)生產(chǎn)者消費(fèi)者問(wèn)題來(lái)說(shuō)明線(xiàn)程問(wèn)題,舉例如下所示:
- /**
- * 生產(chǎn)者消費(fèi)者問(wèn)題
- */
- package com.basic.thread;
- /**
- * @author johnston678
- *
- * @version 2009-05-06
- */
- public class ProducerConsumer {
- /**
- * @param args
- */
- public static void main(String[] args) {
- ProductBox pb = new ProductBox();
- Producer p = new Producer(pb);
- Consumer c = new Consumer(pb);
- Thread pThread = new Thread(p);
- Thread cThread = new Thread(c);
- pThread.setPriority(Thread.MAX_PRIORITY);
- pThread.start();
- cThread.start();
- }
- }
- /**
- * 產(chǎn)品對(duì)象
- * @author johsnton678
- */
- class Product {
- int id;
- public Product(int id) {
- super();
- this.id = id;
- }
- public String toString(){
- return "Product:" + id;
- }
- }
- /**
- * 產(chǎn)品盒對(duì)象
- * @author johnston678
- */
- class ProductBox {
- Product[] productbox = new Product[6];
- int index = 0;
- public ProductBox() {
- super();
- }
- public synchronized void push(Product p) {
- while (index == productbox.length) {
- try {
- this.wait();
- } catch (InterruptedException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- this.notify();
- productbox[index] = p;
- index ++;
- }
- public synchronized Product pop() {
- while (index == 0) {
- try {
- this.wait();
- } catch (InterruptedException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- this.notify();
- index --;
- return productbox[index];
- }
- }
- /**
- * 生產(chǎn)者
- * @author johnston678
- */
- class Producer implements Runnable {
- ProductBox productbox = null;
- public Producer(ProductBox productbox) {
- super();
- this.productbox = productbox;
- }
- @Override
- public void run() {
- // TODO Auto-generated method stub
- for (int i=0; i<10; i++) {
- Product p = new Product(i);
- productbox.push(p);
- System.out.println("produce:" + p);
- try {
- Thread.sleep((int)(Math.random() * 200));
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- }
- }
- }
- /**
- * 消費(fèi)者
- * @author johnston678
- */
- class Consumer implements Runnable {
- ProductBox productbox = null;
- public Consumer(ProductBox productbox) {
- super();
- this.productbox = productbox;
- }
- @Override
- public void run() {
- // TODO Auto-generated method stub
- for (int i=0; i<10; i++) {
- Product p = productbox.pop();
- System.out.println("consume:" + p);
- try {
- Thread.sleep((int)(Math.random() * 1000));
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- }
- }
- }
【編輯推薦】