自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

Java多線(xiàn)程的相關(guān)機(jī)制

開(kāi)發(fā) 后端
本文講述了Java多線(xiàn)程的相關(guān)機(jī)制,分為線(xiàn)程的基本概念、線(xiàn)程的創(chuàng)建和啟動(dòng)、線(xiàn)程控制的基本方法和線(xiàn)程同步四個(gè)方面進(jìn)行講解。

一 線(xiàn)程的基本概念

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

二 線(xiàn)程的創(chuàng)建和啟動(dòng)

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

三 線(xiàn)程控制的基本方法

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

四 線(xiàn)程同步

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

  1. /**  
  2. * 生產(chǎn)者消費(fèi)者問(wèn)題  
  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. * 產(chǎn)品對(duì)象  
  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. * 產(chǎn)品盒對(duì)象  
  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. * 生產(chǎn)者  
  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. * 消費(fèi)者  
  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.       

 

【編輯推薦】

  1. 20個(gè)開(kāi)發(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ù)
責(zé)任編輯:仲衡 來(lái)源: 小川的博客
相關(guān)推薦

2010-03-17 19:24:38

Java多線(xiàn)程循環(huán)

2010-01-21 11:27:30

linux多線(xiàn)程機(jī)制線(xiàn)程同步

2010-03-16 19:29:26

Java多線(xiàn)程操作

2009-09-02 14:00:34

C#文件處理

2010-03-16 18:40:59

Java多線(xiàn)程編程

2010-03-15 16:31:34

Java多線(xiàn)程

2009-07-03 17:18:34

Servlet多線(xiàn)程

2023-10-08 09:34:11

Java編程

2023-06-09 07:59:37

多線(xiàn)程編程鎖機(jī)制

2010-03-16 17:52:27

Java多線(xiàn)程信號(hào)量

2009-09-01 17:15:42

C#多線(xiàn)程應(yīng)用

2009-03-12 10:52:43

Java線(xiàn)程多線(xiàn)程

2024-07-05 08:32:36

2010-03-16 10:50:21

Java多線(xiàn)程服務(wù)器

2021-09-11 15:26:23

Java多線(xiàn)程線(xiàn)程池

2021-12-26 18:22:30

Java線(xiàn)程多線(xiàn)程

2009-06-29 17:49:47

Java多線(xiàn)程

2024-05-06 00:00:01

鎖定機(jī)制編程

2024-06-28 08:45:58

2009-06-11 17:03:29

Java線(xiàn)程
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)