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

RELATEED CONSULTING
相關咨詢
選擇下列產品馬上在線溝通
服務時間:8:30-17:00
你可能遇到了下面的問題
關閉右側工具欄

新聞中心

這里有您想知道的互聯(lián)網營銷解決方案
Java多線程的相關機制

一 線程的基本概念

成都創(chuàng)新互聯(lián)為您提適合企業(yè)的網站設計?讓您的網站在搜索引擎具有高度排名,讓您的網站具備超強的網絡競爭力!結合企業(yè)自身,進行網站設計及把握,最后結合企業(yè)文化和具體宗旨等,才能創(chuàng)作出一份性化解決方案。從網站策劃到做網站、成都網站建設, 我們的網頁設計師為您提供的解決方案。

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

二 線程的創(chuàng)建和啟動

可以有兩種方式創(chuàng)建新的線程:
***種:
     1.定義線程類實現(xiàn)Runnable接口
     2.Thread myThread = new Thread(target);   //target為Runnable接口類型
     3.Runnable中只有一個方法:public void run();用以定義線程運行體
     4.使用Runnable接口可以為多個線程提供共享的數(shù)據
     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();

三 線程控制的基本方法

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

四 線程同步

實現(xiàn)生產者消費者問題來說明線程問題,舉例如下所示:

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

 


網頁標題:Java多線程的相關機制
文章地址:http://m.5511xx.com/article/cdgsiij.html