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线程池调度器

2010-03-17 15:45:06

Java多线程求和

2011-06-22 13:57:54

Java多线程

2012-02-15 10:34:29

JavaJava Socket

2012-05-18 10:36:20

CC++编程

2010-07-26 13:27:19

Perl多线程

2013-07-16 10:57:34

iOS多线程多线程概念多线程入门

2009-03-12 10:52:43

Java线程多线程

2010-02-05 15:30:54

C++多线程测试

2021-12-26 18:22:30

Java线程多线程

2009-06-29 17:49:47

Java多线程

2017-12-18 16:33:55

多线程对象模型

2009-10-23 09:26:09

VB.NET多线程

2010-03-15 17:56:23

Java多线程

2016-11-10 16:30:22

Java多线程

2012-09-21 13:30:51

Java多线程Java发展
点赞
收藏

51CTO技术栈公众号