日韩无码专区无码一级三级片|91人人爱网站中日韩无码电影|厨房大战丰满熟妇|AV高清无码在线免费观看|另类AV日韩少妇熟女|中文日本大黄一级黄色片|色情在线视频免费|亚洲成人特黄a片|黄片wwwav色图欧美|欧亚乱色一区二区三区

RELATEED CONSULTING
相關(guān)咨詢
選擇下列產(chǎn)品馬上在線溝通
服務(wù)時(shí)間:8:30-17:00
你可能遇到了下面的問(wèn)題
關(guān)閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營(yíng)銷解決方案
Java多線程學(xué)習(xí)總結(jié)(二)

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

我們提供的服務(wù)有:成都網(wǎng)站設(shè)計(jì)、做網(wǎng)站、微信公眾號(hào)開(kāi)發(fā)、網(wǎng)站優(yōu)化、網(wǎng)站認(rèn)證、鹽山ssl等。為上千余家企事業(yè)單位解決了網(wǎng)站和推廣的問(wèn)題。提供周到的售前咨詢和貼心的售后服務(wù),是有科學(xué)管理、有技術(shù)的鹽山網(wǎng)站制作公司

 
 
 
  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. class MyThread extends Thread{
  12.     public void run(){
  13.         while(true){
  14.             try{
  15.                 System.out.println("------"+new Date()+"-----");
  16.                 Thread.sleep(1000);
  17.             }catch(InterruptedException i){
  18.                 return;
  19.             }
  20.         }
  21.     }
  22. }

二、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è)可稱為"互斥鎖"的標(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. class WoTou {
  13.     int id; 
  14.     WoTou(int id) {
  15.         this.id = id;
  16.     }
  17.     public String toString() {
  18.         return "WoTou : " + id;
  19.     }
  20. }
  21. class SyncStack {        //棧實(shí)現(xiàn)
  22.     int index = 0;
  23.     WoTou[] arrWT = new WoTou[6];    //相當(dāng)于裝物品的籃子
  24.     
  25.     public synchronized void push(WoTou wt) {    //生產(chǎn)物品,線程安全
  26.         while(index == arrWT.length) {        //當(dāng)籃子滿了線程等待
  27.             try {            
  28.                 this.wait();        
  29.             } catch (InterruptedException e) {
  30.                 e.printStackTrace();
  31.             }
  32.             
  33.         }
  34.         this.notifyAll();    //開(kāi)始生產(chǎn)時(shí),叫醒等待的其他線程開(kāi)始消費(fèi)
  35.         arrWT[index] = wt;    
  36.         index ++;
  37.     }
  38.     
  39.     public synchronized WoTou pop() {        //消費(fèi)物品,線程安全
  40.         while(index == 0) {            //如果籃子空了
  41.             try {
  42.                 this.wait();        //線程等待,等待生產(chǎn)者開(kāi)始                         
  43. //生產(chǎn),叫醒此線程
  44.             } catch (InterruptedException e) {
  45.                 e.printStackTrace();
  46.             }
  47.             
  48.         }
  49.         this.notifyAll();            //消費(fèi)時(shí)喊醒生產(chǎn)者生產(chǎn)
  50.         index--;
  51.         return arrWT[index];
  52.     }
  53. }
  54. class Producer implements Runnable {            //生產(chǎn)者類
  55.     SyncStack ss = null;
  56.     Producer(SyncStack ss) {
  57.         this.ss = ss;
  58.     }
  59.     
  60.     public void run() {
  61.         for(int i=0; i<20; i++) {    //生產(chǎn)20個(gè)
  62.             WoTou wt = new WoTou(i);
  63.             ss.push(wt);            
  64.             System.out.println("生產(chǎn)了:" + wt);
  65.             try {
  66.                 Thread.sleep((int)(Math.random() * 200));
  67.             } catch (InterruptedException e) {
  68.                 e.printStackTrace();
  69.             }            
  70.         }
  71.     }
  72. }
  73. class Consumer implements Runnable {
  74.     SyncStack ss = null;
  75.     Consumer(SyncStack ss) {
  76.         this.ss = ss;
  77.     }
  78.     
  79.     public void run() {
  80.         for(int i=0; i<20; i++) {        //消費(fèi)20個(gè)
  81.             WoTou wt = ss.pop();
  82.             System.out.println("消費(fèi)了: " + wt);
  83.             try {
  84.                 Thread.sleep((int)(Math.random() * 1000));
  85.             } catch (InterruptedException e) {
  86.                 e.printStackTrace();
  87.             }            
  88.         }
  89.     }
  90. }

分享標(biāo)題:Java多線程學(xué)習(xí)總結(jié)(二)
當(dāng)前鏈接:http://m.5511xx.com/article/djhdhip.html