日韩无码专区无码一级三级片|91人人爱网站中日韩无码电影|厨房大战丰满熟妇|AV高清无码在线免费观看|另类AV日韩少妇熟女|中文日本大黄一级黄色片|色情在线视频免费|亚洲成人特黄a片|黄片wwwav色图欧美|欧亚乱色一区二区三区

RELATEED CONSULTING
相關(guān)咨詢
選擇下列產(chǎn)品馬上在線溝通
服務(wù)時(shí)間:8:30-17:00
你可能遇到了下面的問題
關(guān)閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
有關(guān)Java線程機(jī)制的淺析

一 線程的基本概念:
線程是一個(gè)程序內(nèi)部的順序控制流,一個(gè)進(jìn)程相當(dāng)于一個(gè)任務(wù),一個(gè)線程相當(dāng)于一個(gè)任務(wù)中的一條執(zhí)行路徑。多進(jìn)程:在操作系統(tǒng)中能同時(shí)運(yùn)行多個(gè)任務(wù)(程序);多線程:在同一個(gè)應(yīng)用程序中有多個(gè)順序流同時(shí)執(zhí)行;Java線程是通過java.lang.Thread類來實(shí)現(xiàn)的;VM啟動(dòng)時(shí)會(huì)有一個(gè)由主方法(public static void main(){})所定義的線程;以通過創(chuàng)建Thread的實(shí)例來創(chuàng)建新的線程
每個(gè)線程都是通過某個(gè)特定Thread對(duì)象所對(duì)應(yīng)的方法run()來完成其操作的,方法run()稱為線程體
通過調(diào)用Thread類的start()方法來啟動(dòng)一個(gè)線程

創(chuàng)新互聯(lián)建站是專業(yè)的溫縣網(wǎng)站建設(shè)公司,溫縣接單;提供成都網(wǎng)站建設(shè)、做網(wǎng)站,網(wǎng)頁設(shè)計(jì),網(wǎng)站設(shè)計(jì),建網(wǎng)站,PHP網(wǎng)站建設(shè)等專業(yè)做網(wǎng)站服務(wù);采用PHP框架,可快速的進(jìn)行溫縣網(wǎng)站開發(fā)網(wǎng)頁制作和功能擴(kuò)展;專業(yè)做搜索引擎喜愛的網(wǎng)站,專業(yè)的做網(wǎng)站團(tuán)隊(duì),希望更多企業(yè)前來合作!

二 Java線程的創(chuàng)建和啟動(dòng):
可以有兩種方式創(chuàng)建新的線程:
第一種:
     1.定義線程類實(shí)現(xiàn)Runnable接口
     2.Thread myThread = new Thread(target);   //target為Runnable接口類型
     3.Runnable中只有一個(gè)方法:public void run();用以定義線程運(yùn)行體
     4.使用Runnable接口可以為多個(gè)線程提供共享的數(shù)據(jù)
     5.在實(shí)現(xiàn)Runnable接口的類的run()方法定義中可以使用Thread的靜態(tài)方法public static Thread currentThread();獲取當(dāng)前線程的引用
   
第二種:
      1.可以定義一個(gè)Thread的子類并重寫其run方法如:
          class MyThread extends Thread {   
              public void run() {...}
            
          }   
     2.然后生成該類的對(duì)象:
         MyThread myThread = new MyThread();

三 Java線程控制的基本方法:
isAlive():判斷線程是否還"活"著
getPriority():獲得線程的優(yōu)先級(jí)數(shù)值
setPriority():設(shè)置線程的優(yōu)先級(jí)數(shù)值
Thread.sleep():將當(dāng)前線程睡眠指定毫秒數(shù)
join():調(diào)用某線程的該方法,將當(dāng)前線程與該線程"合并",即等待該線程結(jié)束,再恢復(fù)當(dāng)前線程的運(yùn)行
yield():讓出cpu,當(dāng)前線程進(jìn)入就緒隊(duì)列等待調(diào)度
wait():當(dāng)前線程進(jìn)入對(duì)象的wait pool
notify()/notifyAll():喚醒對(duì)象的wait pool中的一個(gè)/所有等待線程

四 線程同步:
實(shí)現(xiàn)生產(chǎn)者消費(fèi)者問題來說明線程問題,舉例如下所示:

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

【編輯推薦】

  1. 20個(gè)開發(fā)人員非常有用的Java功能代碼
  2. 走進(jìn)Java 7中的模塊系統(tǒng)
  3. JavaFX 1.2 已經(jīng)發(fā)布 主要新功能一覽
  4. 2009年十大Java技術(shù)解決方案
  5. 2008最值得學(xué)習(xí)的五種JAVA技術(shù)

分享題目:有關(guān)Java線程機(jī)制的淺析
文章URL:http://www.5511xx.com/article/dpehpdo.html