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

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

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

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

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

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

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

  1. /**  
  2. * 生產(chǎn)者消費者問題  
  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)品對象  
  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)品盒對象  
  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. * 消費者  
  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個開發(fā)人員非常有用的Java功能代碼
  2. 走進Java 7中的模塊系統(tǒng)
  3. JavaFX 1.2 已經(jīng)發(fā)布 主要新功能一覽
  4. 2009年十大Java技術(shù)解決方案
  5. 2008最值得學(xué)習的五種JAVA技術(shù)
責任編輯:仲衡 來源: 小川個人博客
相關(guān)推薦

2009-06-23 14:15:00

Java垃圾回收

2014-08-13 10:41:08

linux線程

2009-06-11 11:17:59

Java多線程

2011-05-19 10:57:45

DNSSEC密鑰加密

2009-04-27 13:15:04

多線程方法run()

2021-08-30 09:44:47

Kubelet機制驅(qū)逐

2016-01-08 10:06:52

2015-08-24 14:59:06

Java線程

2017-04-12 11:46:46

前端瀏覽器渲染機制

2009-07-16 09:54:44

LookupEventSwing線程

2023-07-07 10:41:00

Javajar文件

2009-07-02 09:38:17

Hibernate延時

2009-10-16 10:20:37

Python的GIL

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-07-15 16:03:26

Swing線程

2016-08-23 14:25:19

MySQL約束數(shù)據(jù)庫

2010-06-07 13:30:15

2009-04-08 09:58:07

ASP.NET MVCTempData框架
點贊
收藏

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