有關(guān)Java線程機制的淺析
一 線程的基本概念:
線程是一個程序內(nèi)部的順序控制流,一個進程相當于一個任務(wù),一個線程相當于一個任務(wù)中的一條執(zhí)行路徑。多進程:在操作系統(tǒng)中能同時運行多個任務(wù)(程序);多線程:在同一個應(yīng)用程序中有多個順序流同時執(zhí)行;Java線程是通過java.lang.Thread類來實現(xiàn)的;VM啟動時會有一個由主方法(public static void main(){})所定義的線程;以通過創(chuàng)建Thread的實例來創(chuàng)建新的線程
每個線程都是通過某個特定Thread對象所對應(yīng)的方法run()來完成其操作的,方法run()稱為線程體
通過調(diào)用Thread類的start()方法來啟動一個線程
二 Java線程的創(chuàng)建和啟動:
可以有兩種方式創(chuàng)建新的線程:
第一種:
1.定義線程類實現(xiàn)Runnable接口
2.Thread myThread = new Thread(target); //target為Runnable接口類型
3.Runnable中只有一個方法:public void run();用以定義線程運行體
4.使用Runnable接口可以為多個線程提供共享的數(shù)據(jù)
5.在實現(xiàn)Runnable接口的類的run()方法定義中可以使用Thread的靜態(tài)方法public static Thread currentThread();獲取當前線程的引用
第二種:
1.可以定義一個Thread的子類并重寫其run方法如:
class MyThread extends Thread {
public void run() {...}
}
2.然后生成該類的對象:
MyThread myThread = new MyThread();
三 Java線程控制的基本方法:
isAlive():判斷線程是否還"活"著
getPriority():獲得線程的優(yōu)先級數(shù)值
setPriority():設(shè)置線程的優(yōu)先級數(shù)值
Thread.sleep():將當前線程睡眠指定毫秒數(shù)
join():調(diào)用某線程的該方法,將當前線程與該線程"合并",即等待該線程結(jié)束,再恢復(fù)當前線程的運行
yield():讓出cpu,當前線程進入就緒隊列等待調(diào)度
wait():當前線程進入對象的wait pool
notify()/notifyAll():喚醒對象的wait pool中的一個/所有等待線程
四 線程同步:
實現(xiàn)生產(chǎn)者消費者問題來說明線程問題,舉例如下所示:
- /**
- * 生產(chǎ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)品對象
- * @author johsnton678
- */
- class Product {
- int id;
- public Product(int id) {
- super();
- this.id = id;
- }
- public String toString(){
- return "Product:" + id;
- }
- }
- /**
- * 產(chǎn)品盒對象
- * @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();
- }
- }
- }
- }
- /**
- * 消費者
- * @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();
- }
- }
- }
- }
【編輯推薦】