成人免费xxxxx在线视频软件_久久精品久久久_亚洲国产精品久久久_天天色天天色_亚洲人成一区_欧美一级欧美三级在线观看

Java多線程學習總結(二)

開發 后端
本文是Java多線程學習總結的第二部分,講到了interrupt方法一種讓線程退出的方式、join和yield方法、線程優先級別 、線程優先級別 、線程同步、生產者消費者問題五個方面的內容。

一、interrupt方法一種讓線程退出的方式。

  1. import java.util.*;  
  2. public class TestInterrupt{  
  3.     public static void main(String[] args){  
  4.         MyThread t = new MyThread();  
  5.         t.start();  
  6.         try{Thread.sleep(10000);}  
  7.         catch(InterruptedException i){}  
  8.         t.interrupt();  
  9.     }  
  10. }  
  11.  
  12. class MyThread extends Thread{  
  13.     public void run(){  
  14.         while(true){  
  15.             try{  
  16.                 System.out.println("------"+new Date()+"-----");  
  17.                 Thread.sleep(1000);  
  18.             }catch(InterruptedException i){  
  19.                 return;  
  20.             }  
  21.         }  
  22.     }  
  23. }  
  24.  

二、join和yield方法
 t.join(); //t的run()方法完才會繼續執行當前線程方法體
             //也就是兩個線程變成了一個線程
 t.yield(); //暫停當前正在執行的線程對象,并執行其他線程。方法為靜態
              //哪個線程體執行此方法,哪個線程讓步

  1. public class TestYield {  
  2.   public static void main(String[] args) {  
  3.     MyThread3 t1 = new MyThread3("t1");  
  4.     MyThread3 t2 = new MyThread3("t2");  
  5.     t1.start(); t2.start();  
  6.   }  
  7. }  
  8. class MyThread3 extends Thread {  
  9.   MyThread3(String s){super(s);}  
  10.   public void run(){  
  11.     for(int i =1;i<=100;i++){  
  12.       System.out.println(getName()+": "+i);  
  13.       if(i%10==0){  
  14.         yield();  
  15.       }  
  16.     }  
  17.   }  
  18. }  


三、線程優先級別
 線程的優先級用數字表示,范圍從1到10,一個線程的缺省優先級為5.
 Thread.MAX_PRIORITY=1
 Thread.MIN_PRIORITY=10
 Thread.NORM_PRIORITY=5
 例:t.setPriority(Thread.NORM_PRIORITY+3);
 
四、線程同步
 1.同步代碼塊
 synchronized(this){  //在執行代碼塊過程中,不會被其他線程打斷
  ... 
 }
 public sunchronized void method //執行此方法時,當前對象被鎖定
 在Java語言中,引入了對象互斥鎖的概念,保證共享數據操作的完整性,每個對象 都對應一個可稱為"互斥鎖"的標記,這個標記保證在任一時刻,只能有一個線程訪 問該對象。
 2.線程死鎖


 

  1. public class TestDeadLock implements Runnable {  
  2.     public int flag = 1;  
  3.     static Object o1 = new Object(), o2 = new Object();  
  4.     public void run() {  
  5. System.out.println("flag=" + flag);  
  6.         if(flag == 1) {  
  7.             synchronized(o1) {  
  8.                 try {  
  9.                     Thread.sleep(500);  
  10.                 } catch (Exception e) {  
  11.                     e.printStackTrace();  
  12.                 }  
  13.                 synchronized(o2) {  
  14.                     System.out.println("1");      
  15.                 }  
  16.             }  
  17.         }  
  18.         if(flag == 0) {  
  19.             synchronized(o2) {  
  20.                 try {  
  21.                     Thread.sleep(500);  
  22.                 } catch (Exception e) {  
  23.                     e.printStackTrace();  
  24.                 }  
  25.                 synchronized(o1) {  
  26.                     System.out.println("0");  
  27.                 }  
  28.             }  
  29.         }  
  30.     }      
  31.       
  32.     public static void main(String[] args) {  
  33.         TestDeadLock td1 = new TestDeadLock();  
  34.         TestDeadLock td2 = new TestDeadLock();  
  35.         td1.flag = 1;  
  36.         td2.flag = 0;  
  37.         Thread t1 = new Thread(td1);  
  38.         Thread t2 = new Thread(td2);  
  39.         t1.start();  
  40.         t2.start();  
  41.           
  42.     }  
  43. }  

五、生產者消費者問題

  1. public class ProducerConsumer {  
  2.     public static void main(String[] args) {  
  3.         SyncStack ss = new SyncStack();  
  4.         Producer p = new Producer(ss);  
  5.         Consumer c = new Consumer(ss);  
  6.         new Thread(p).start();  
  7.         new Thread(p).start();  
  8.         new Thread(p).start();  
  9.         new Thread(c).start();  
  10.     }  
  11. }  
  12.  
  13. class WoTou {  
  14.     int id;   
  15.     WoTou(int id) {  
  16.         this.id = id;  
  17.     }  
  18.     public String toString() {  
  19.         return "WoTou : " + id;  
  20.     }  
  21. }  
  22.  
  23. class SyncStack {        //棧實現  
  24.     int index = 0;  
  25.     WoTou[] arrWT = new WoTou[6];    //相當于裝物品的籃子  
  26.       
  27.     public synchronized void push(WoTou wt) {    //生產物品,線程安全  
  28.         while(index == arrWT.length) {        //當籃子滿了線程等待  
  29.             try {              
  30.                 this.wait();          
  31.             } catch (InterruptedException e) {  
  32.                 e.printStackTrace();  
  33.             }  
  34.               
  35.         }  
  36.         this.notifyAll();    //開始生產時,叫醒等待的其他線程開始消費  
  37.         arrWT[index] = wt;      
  38.         index ++;  
  39.     }  
  40.       
  41.     public synchronized WoTou pop() {        //消費物品,線程安全  
  42.         while(index == 0) {            //如果籃子空了  
  43.             try {  
  44.                 this.wait();        //線程等待,等待生產者開始                           
  45. //生產,叫醒此線程  
  46.             } catch (InterruptedException e) {  
  47.                 e.printStackTrace();  
  48.             }  
  49.               
  50.         }  
  51.         this.notifyAll();            //消費時喊醒生產者生產  
  52.         index--;  
  53.         return arrWT[index];  
  54.     }  
  55. }  
  56.  
  57. class Producer implements Runnable {            //生產者類  
  58.     SyncStack ss = null;  
  59.     Producer(SyncStack ss) {  
  60.         this.ss = ss;  
  61.     }  
  62.       
  63.     public void run() {  
  64.         for(int i=0; i<20; i++) {    //生產20個  
  65.             WoTou wt = new WoTou(i);  
  66.             ss.push(wt);              
  67.             System.out.println("生產了:" + wt);  
  68.             try {  
  69.                 Thread.sleep((int)(Math.random() * 200));  
  70.             } catch (InterruptedException e) {  
  71.                 e.printStackTrace();  
  72.             }              
  73.         }  
  74.     }  
  75. }  
  76.  
  77. class Consumer implements Runnable {  
  78.     SyncStack ss = null;  
  79.     Consumer(SyncStack ss) {  
  80.         this.ss = ss;  
  81.     }  
  82.       
  83.     public void run() {  
  84.         for(int i=0; i<20; i++) {        //消費20個  
  85.             WoTou wt = ss.pop();  
  86.             System.out.println("消費了: " + wt);  
  87.             try {  
  88.                 Thread.sleep((int)(Math.random() * 1000));  
  89.             } catch (InterruptedException e) {  
  90.                 e.printStackTrace();  
  91.             }              
  92.         }  
  93.     }  
  94. }  

 

【編輯推薦】

  1. 20個開發人員非常有用的Java功能代碼
  2. 走進Java 7中的模塊系統
  3. JavaFX 1.2 已經發布 主要新功能一覽
  4. 2009年十大Java技術解決方案
  5. 2008最值得學習的五種JAVA技術
責任編輯:仲衡 來源: 小舒的Java世界
相關推薦

2009-06-11 10:22:18

Java多線程

2010-03-16 19:29:26

Java多線程操作

2015-12-22 10:39:52

Java多線程問題

2017-01-19 10:24:38

Java多線程問題

2013-06-08 13:07:23

Java線程池調度器

2012-05-18 10:36:20

CC++編程

2010-03-17 15:45:06

Java多線程求和

2012-02-15 10:34:29

JavaJava Socket

2011-06-22 13:57:54

Java多線程

2010-07-26 13:27:19

Perl多線程

2013-07-16 10:57:34

iOS多線程多線程概念多線程入門

2010-02-05 15:30:54

C++多線程測試

2009-03-12 10:52:43

Java線程多線程

2009-10-23 09:26:09

VB.NET多線程

2009-06-29 17:49:47

Java多線程

2021-12-26 18:22:30

Java線程多線程

2017-12-18 16:33:55

多線程對象模型

2009-08-28 16:43:57

C#多線程學習

2009-08-03 11:07:18

Scala Actor

2011-08-18 17:07:23

IOS開發多線程NSInvocatio
點贊
收藏

51CTO技術棧公眾號

主站蜘蛛池模板: 天天操网| caoporn国产精品免费公开 | 激情久久av一区av二区av三区 | 日韩一区二区免费视频 | 精品99久久久久久 | 在线一区视频 | 日本免费一区二区三区四区 | 岛国二区 | 人人干人人爽 | www.天天干.com | 五月天国产 | 国产成人精品免费 | 国产三级大片 | 日韩中出| 成人水多啪啪片 | 久久久久久久久久性 | 国际精品鲁一鲁一区二区小说 | 中文在线视频 | 精品国产乱码久久久久久闺蜜 | 欧美精品一区二区三区在线 | 99国产视频| 99福利视频 | 天天操天天干天天爽 | 91麻豆精品国产91久久久久久 | 99热视| 日本国产一区二区 | 国产黑丝在线 | av成人在线观看 | 操人网 | 欧美激情精品久久久久久变态 | 久久黄网 | 91精品久久久久久久久久 | 国产精品揄拍一区二区 | 逼逼视频 | www国产亚洲精品 | 久久精品久久久久久 | 天天干天天爽 | 黄色三级免费网站 | 成人在线视频网 | 国产欧美在线观看 | 国产精品毛片无码 |