有关Java线程机制的浅析(转载)

一 线程的基本概念:
线程是一个程序内部的顺序控制流,一个进程相当于一个任务,一个线程相当于一个任务中的一条执行路径。多进程:在操作系统中能同时运行多个任务(程序);多线程:在同一个应用程序中有多个顺序流同时执行;Java线程是通过java.lang.Thread类来实现的;VM启动时会有一个由主方法(public static void main(){})所定义的线程;以通过创建Thread的实例来创建新的线程
每个线程都是通过某个特定Thread对象所对应的方法run()来完成其操作的,方法run()称为线程体
通过调用Thread类的start()方法来启动一个线程

二 Java线程的创建和启动:
可以有两种方式创建新的线程:
第一种:
1.定义线程类实现Runnable接口
2.Thread myThread = new Thread(target);   //target为Runnable接口类型
3.Runnable中只有一个方法:public void run();用以定义线程运行体
4.使用Runnable接口可以为多个线程提供共享的数据
5.在实现Runnable接口的类的run()方法定义中可以使用Thread的静态方法public static Thread currentThread();获取当前线程的引用

第二种:
1.可以定义一个Thread的子类并重写其run方法如:
class MyThread extends Thread {    
public void run() {...}

}    
2.然后生成该类的对象:
MyThread myThread = new MyThread();

三 Java线程控制的基本方法:
isAlive():判断线程是否还"活"着
getPriority():获得线程的优先级数值
setPriority():设置线程的优先级数值
Thread.sleep():将当前线程睡眠指定毫秒数
join():调用某线程的该方法,将当前线程与该线程"合并",即等待该线程结束,再恢复当前线程的运行
yield():让出cpu,当前线程进入就绪队列等待调度
wait():当前线程进入对象的wait pool
notify()/notifyAll():唤醒对象的wait pool中的一个/所有等待线程

四 线程同步:
实现生产者消费者问题来说明线程问题,举例如下所示:

  1 public class ProducerConsumer {
  2 
  3     /**
  4      * @param args
  5      */
  6     public static void main(String[] args) {
  7         ProductBox pb = new ProductBox();
  8         Producer p = new Producer(pb);
  9         Consumer c = new Consumer(pb);
 10 
 11         Thread pThread = new Thread(p);
 12         Thread cThread = new Thread(c);
 13         pThread.setPriority(Thread.MAX_PRIORITY);
 14 
 15         pThread.start();
 16         cThread.start();
 17     }
 18 
 19 }
 20 
 21 /**
 22  * 产品对象
 23  * 
 24  * @author johsnton678
 25  */
 26 class Product {
 27     int id;
 28 
 29     public Product(int id) {
 30         super();
 31         this.id = id;
 32     }
 33 
 34     @Override
 35     public String toString() {
 36         return "Product [id=" + id + "]";
 37     }
 38 
 39     
 40 }
 41 
 42 /**
 43  * 产品盒对象
 44  * 
 45  * @author johnston678
 46  */
 47 class ProductBox {
 48 
 49     Product[] productbox = new Product[6];
 50     int index = 0;
 51 
 52     public ProductBox() {
 53         super();
 54     }
 55 
 56     public synchronized void push(Product p) {
 57         while (index == productbox.length) {
 58             try {
 59                 this.wait();
 60             } catch (InterruptedException e) {
 61                 // TODO Auto-generated catch block
 62                 e.printStackTrace();
 63             }
 64         }
 65         this.notify();
 66         productbox[index] = p;
 67         index++;
 68     }
 69 
 70     public synchronized Product pop() {
 71         while (index == 0) {
 72             try {
 73                 this.wait();
 74             } catch (InterruptedException e) {
 75                 // TODO Auto-generated catch block
 76                 e.printStackTrace();
 77             }
 78         }
 79         this.notify();
 80         index--;
 81         return productbox[index];
 82 
 83     }
 84 }
 85 
 86 /**
 87  * 生产者
 88  * 
 89  * @author johnston678
 90  */
 91 class Producer implements Runnable {
 92 
 93     ProductBox productbox = null;
 94 
 95     public Producer(ProductBox productbox) {
 96         super();
 97         this.productbox = productbox;
 98     }
 99 
100     @Override
101     public void run() {
102         // TODO Auto-generated method stub
103         for (int i = 0; i < 10; i++) {
104             Product p = new Product(i);
105             productbox.push(p);
106             System.out.println(">>>>>produce:" + p);
107 
108             try {
109                 Thread.sleep((int) (Math.random() * 200));
110             } catch (InterruptedException e) {
111                 e.printStackTrace();
112             }
113         }
114     }
115 
116 }
117 
118 /**
119  * 消费者
120  * 
121  * @author johnston678
122  */
123 class Consumer implements Runnable {
124 
125     ProductBox productbox = null;
126 
127     public Consumer(ProductBox productbox) {
128         super();
129         this.productbox = productbox;
130     }
131 
132     @Override
133     public void run() {
134         // TODO Auto-generated method stub
135         for (int i = 0; i < 10; i++) {
136             Product p = productbox.pop();
137             System.out.println("consume<<<<<:" + p);
138 
139             try {
140                 Thread.sleep((int) (Math.random() * 1000));
141             } catch (InterruptedException e) {
142                 e.printStackTrace();
143             }
144         }
145     }
146 
147 }

原文地址:http://developer.51cto.com/art/200906/128406.htm

posted @ 2013-03-17 23:16  Agrimony  阅读(95)  评论(0)    收藏  举报