偷偷摘套内射激情视频,久久精品99国产国产精,中文字幕无线乱码人妻,中文在线中文a,性爽19p

有關(guān)Java線程機(jī)制的淺析

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

一 線程的基本概念:
線程是一個(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è)線程

二 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():?jiǎn)拘褜?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ù)
責(zé)任編輯:仲衡 來源: 小川個(gè)人博客
相關(guān)推薦

2009-06-23 14:15:00

Java垃圾回收

2014-08-13 10:41:08

linux線程

2016-01-08 10:06:52

2011-05-19 10:57:45

DNSSEC密鑰加密

2017-04-12 11:46:46

前端瀏覽器渲染機(jī)制

2021-08-30 09:44:47

Kubelet機(jī)制驅(qū)逐

2009-07-16 09:54:44

LookupEventSwing線程

2009-06-11 11:17:59

Java多線程

2009-04-27 13:15:04

多線程方法run()

2015-08-24 14:59:06

Java線程

2023-07-07 10:41:00

Javajar文件

2022-06-01 16:01:58

MySQL內(nèi)存管理系統(tǒng)

2023-06-23 15:22:28

JettyJava

2009-07-03 17:18:34

Servlet多線程

2009-10-16 10:20:37

Python的GIL

2009-07-02 09:38:17

Hibernate延時(shí)

2010-02-01 17:25:09

Python多線程

2009-07-15 16:03:26

Swing線程

2010-06-07 13:30:15

2016-08-23 14:25:19

MySQL約束數(shù)據(jù)庫
點(diǎn)贊
收藏

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