当前位置:天才代写 > tutorial > JAVA 教程 > 浅析Java的线程机制

浅析Java的线程机制

2017-11-12 08:00 星期日 所属: JAVA 教程 浏览:493

副标题#e#

一 线程的根基观念:

线程是一个措施内部的顺序节制流,一个历程相当于一个任务,一个线程相当于一个任务中的一条执行路径。多历程:在操纵系统中能同时运行多个任务(措施);多线程:在同一个应用措施中有多个顺序流同时执行;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中的一个/所有期待线程


#p#副标题#e#

四 线程同步:

实现出产者消费者问题来说明线程问题,举譬喻下所示:

/**
* 出产者消费者问题
*/
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();
     }

}

/**
* 产物工具
* @author johsnton678
*/
class Product {
     int id;

     public Product(int id) {
         super();
         this.id = id;
     }

     public String toString(){
         return "Product:" + id;
     }
}

/**
* 产物盒工具
* @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];

     }
}

/**
* 出产者
* @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();
             }
         }
     }

}

 

    关键字:

[contact-form-7 id="17097" title="form2"]天才代写-代写联系方式