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

Java多線程學(xué)習(xí)總結(jié)(二)

開(kāi)發(fā) 后端
本文是Java多線程學(xué)習(xí)總結(jié)的第二部分,講到了interrupt方法一種讓線程退出的方式、join和yield方法、線程優(yōu)先級(jí)別 、線程優(yōu)先級(jí)別 、線程同步、生產(chǎn)者消費(fèi)者問(wèn)題五個(gè)方面的內(nèi)容。

一、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()方法完才會(huì)繼續(xù)執(zhí)行當(dāng)前線程方法體
             //也就是兩個(gè)線程變成了一個(gè)線程
 t.yield(); //暫停當(dāng)前正在執(zhí)行的線程對(duì)象,并執(zhí)行其他線程。方法為靜態(tài)
              //哪個(gè)線程體執(zhí)行此方法,哪個(gè)線程讓步

  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. }  


三、線程優(yōu)先級(jí)別
 線程的優(yōu)先級(jí)用數(shù)字表示,范圍從1到10,一個(gè)線程的缺省優(yōu)先級(jí)為5.
 Thread.MAX_PRIORITY=1
 Thread.MIN_PRIORITY=10
 Thread.NORM_PRIORITY=5
 例:t.setPriority(Thread.NORM_PRIORITY+3);
 
四、線程同步
 1.同步代碼塊
 synchronized(this){  //在執(zhí)行代碼塊過(guò)程中,不會(huì)被其他線程打斷
  ... 
 }
 public sunchronized void method //執(zhí)行此方法時(shí),當(dāng)前對(duì)象被鎖定
 在Java語(yǔ)言中,引入了對(duì)象互斥鎖的概念,保證共享數(shù)據(jù)操作的完整性,每個(gè)對(duì)象 都對(duì)應(yīng)一個(gè)可稱(chēng)為"互斥鎖"的標(biāo)記,這個(gè)標(biāo)記保證在任一時(shí)刻,只能有一個(gè)線程訪 問(wèn)該對(duì)象。
 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. }  

五、生產(chǎn)者消費(fèi)者問(wèn)題

  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 {        //棧實(shí)現(xiàn)  
  24.     int index = 0;  
  25.     WoTou[] arrWT = new WoTou[6];    //相當(dāng)于裝物品的籃子  
  26.       
  27.     public synchronized void push(WoTou wt) {    //生產(chǎn)物品,線程安全  
  28.         while(index == arrWT.length) {        //當(dāng)籃子滿了線程等待  
  29.             try {              
  30.                 this.wait();          
  31.             } catch (InterruptedException e) {  
  32.                 e.printStackTrace();  
  33.             }  
  34.               
  35.         }  
  36.         this.notifyAll();    //開(kāi)始生產(chǎn)時(shí),叫醒等待的其他線程開(kāi)始消費(fèi)  
  37.         arrWT[index] = wt;      
  38.         index ++;  
  39.     }  
  40.       
  41.     public synchronized WoTou pop() {        //消費(fèi)物品,線程安全  
  42.         while(index == 0) {            //如果籃子空了  
  43.             try {  
  44.                 this.wait();        //線程等待,等待生產(chǎn)者開(kāi)始                           
  45. //生產(chǎn),叫醒此線程  
  46.             } catch (InterruptedException e) {  
  47.                 e.printStackTrace();  
  48.             }  
  49.               
  50.         }  
  51.         this.notifyAll();            //消費(fèi)時(shí)喊醒生產(chǎn)者生產(chǎn)  
  52.         index--;  
  53.         return arrWT[index];  
  54.     }  
  55. }  
  56.  
  57. class Producer implements Runnable {            //生產(chǎn)者類(lèi)  
  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++) {    //生產(chǎn)20個(gè)  
  65.             WoTou wt = new WoTou(i);  
  66.             ss.push(wt);              
  67.             System.out.println("生產(chǎn)了:" + 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++) {        //消費(fèi)20個(gè)  
  85.             WoTou wt = ss.pop();  
  86.             System.out.println("消費(fèi)了: " + wt);  
  87.             try {  
  88.                 Thread.sleep((int)(Math.random() * 1000));  
  89.             } catch (InterruptedException e) {  
  90.                 e.printStackTrace();  
  91.             }              
  92.         }  
  93.     }  
  94. }  

 

【編輯推薦】

  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)源: 小舒的Java世界
相關(guān)推薦

2009-06-11 10:22:18

Java多線程

2010-03-16 19:29:26

Java多線程操作

2015-12-22 10:39:52

Java多線程問(wèn)題

2017-01-19 10:24:38

Java多線程問(wèn)題

2013-06-08 13:07:23

Java線程池調(diào)度器

2010-03-17 15:45:06

Java多線程求和

2012-05-18 10:36:20

CC++編程

2011-06-22 13:57:54

Java多線程

2012-02-15 10:34:29

JavaJava Socket

2010-07-26 13:27:19

Perl多線程

2013-07-16 10:57:34

iOS多線程多線程概念多線程入門(mén)

2010-02-05 15:30:54

C++多線程測(cè)試

2009-03-12 10:52:43

Java線程多線程

2017-12-18 16:33:55

多線程對(duì)象模型

2009-10-23 09:26:09

VB.NET多線程

2021-12-26 18:22:30

Java線程多線程

2009-06-29 17:49:47

Java多線程

2011-08-18 17:07:23

IOS開(kāi)發(fā)多線程NSInvocatio

2009-08-03 11:07:18

Scala Actor

2009-08-28 16:43:57

C#多線程學(xué)習(xí)
點(diǎn)贊
收藏

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