zoukankan      html  css  js  c++  java
  • java多线程详解

    转自:线程间通信、等待唤醒机制、生产者消费者问题(Lock,Condition)、停止线程和守护线程、线程优先级

    1  线程间通信

    1.1  线程间通信

    其实就是多个线程在操作同一个资源,但是操作的动作不同。

    比如一个线程给一个变量赋值,而另一个线程打印这个变量。

    1.2  等待唤醒机制

    wait()将线程等待,释放了CPU执行权,同时将线程对象存储到线程池中。

    notify():唤醒线程池中一个等待的线程,若线程池有多个等待的线程,则任意唤醒一个。

    notifyAll():唤醒线程池中,所有的等待中的线程。

    这三个方法都要使用在同步中,因为要对持有锁的线程进行操作。

    比如,A锁上的线程被wait了,那么这个线程就进入了A锁的线程池中,只能被A锁的notify唤醒,而不能被不同锁的其他线程唤醒。

    所以这三个方法要使用在同步中,因为只有同步才具有锁。

    而锁可以是任意对象,这三个方法被锁调用,所以这三个方法可以被任意对象调用,所以这三个方法定义在Object类中。

    wait()sleep()的区别:

    wait():可以指定等待的时间,也可以不指定时间,如果不指定时间,就只能被同一个锁的notifynotifyAll唤醒。wait时线程会释放CPU执行权,并且会释放锁。

    sleep():必须指定线程休眠的时间,线程休眠即暂停执行。时间到了,线程就自动苏醒,恢复运行。sleep时线程会释放执行权,但不释放锁。

    线程的停止:

    1,如果run()方法中定义了循环,可以用循环结束标记,跳出循环,则线程就停止了。 

    2,如果线程已被冻结,读不到循环结束标记,则需要通过Thread类的interrupt方法中断线程,让线程重新获得执行的资格,从而可以读到循环结束标记,而结束线程。

    3setDaemon(true)方法将当前线程标记为守护线程,当运行的线程都是守护线程时,则Java虚拟机退出。该方法必须在启动线程前调用。

    等待唤醒机制代码,实现两个线程交替执行,在控制台上交替打印两个字符串。

    等待和唤醒是同一个锁r

    [java] view plaincopy在CODE上查看代码片派生到我的代码片
     
    1. //两个线程交替执行,在控制台交替打印两串字符串。  
    2. class Res{  
    3.     String name;  
    4.     String sex;  
    5.     boolean flag = false//等待唤醒机制  
    6. }  
    7. class Input implements Runnable{  
    8.     private Res r;  
    9.     Input(Res r){  
    10.         this.r = r;  
    11.     }  
    12.     public void run(){  
    13.         int x = 0;  
    14.         while(true){  
    15.             synchronized(r){  //等待和唤醒,是同一个锁。  
    16.                 if(r.flag)    //等待唤醒机制,true则等待,false则执行  
    17.                     try{r.wait();}catch(Exception e){}  //线程等待,进入线程池  
    18.                 if(x == 0){  
    19.                     r.name = "LuoQi";  
    20.                     r.sex = "man";  
    21.                 }  
    22.                 else{  
    23.                     r.name = "丽丽";  //赋值时,赋值了name还没赋值sex,就打印“lili----male”,加同步锁,牢记同步前提。  
    24.                     r.sex = "女";  
    25.                 }  
    26.                 x = (x+1)%2;  
    27.                 r.flag = true;  //等待唤醒机制  
    28.                 r.notify();  //任意唤醒线程池里的一个被等待的线程  //等待唤醒机制  
    29.             }  
    30.         }  
    31.     }  
    32. }  
    33. class Output implements Runnable{  
    34.     private Res r;  
    35.     Output(Res r){  
    36.         this.r = r;  
    37.     }  
    38.     public void run(){  
    39.         while(true){  
    40.             synchronized(r){  //等待和唤醒,是同一个锁。  
    41.                 if(!r.flag)   //等待唤醒机制,false则等待,true则执行  
    42.                     try{r.wait();}catch(Exception e){}  //线程等待,进入线程池  
    43.                 System.out.println(r.name+"----"+r.sex);  
    44.                 r.flag = false;  //等待唤醒机制  
    45.                 r.notify();  //唤醒Input线程  //等待唤醒机制  
    46.             }  
    47.         }  
    48.     }  
    49. }  
    50. class ThreadCommunication{  
    51.     public static void main(String[] args){  
    52.         Res r = new Res();  
    53.           
    54.         Input in = new Input(r);  
    55.         Output out = new Output(r);  
    56.           
    57.         Thread t1 = new Thread(in);  
    58.         Thread t2 = new Thread(out);  
    59.         t1.start();  
    60.         t2.start();  
    61.     }  
    62. }  

    以上代码中,把两个同步代码块中的代码,封装成两个同步方法,一个更改两个字段的值,另一个打印两个字段的值。

    两个同步方法写在Res类中,这样同步锁都是Res.class字节码文件,保证了等待和唤醒是同一个锁:

    [java] view plaincopy在CODE上查看代码片派生到我的代码片
     
    1. //两个线程交替执行,在控制台交替打印两串字符串。  
    2. class Res{  
    3.     String name;  
    4.     String sex;  
    5.     boolean flag = false;   
    6.       
    7.     public synchronized void setRes(String name,String sex){ //同步函数  
    8.         if(this.flag)    //flag为true,则线程等待进入线程池。  
    9.             try{this.wait();}catch(Exception e){}   
    10.         this.name = name;  //flag为false,则线程继续执行。  
    11.         this .sex = sex;  
    12.         this.flag = true;  
    13.         this.notify();   //任意唤醒线程池中一个等待的线程  
    14.     }  
    15.     public synchronized void getRes(){  
    16.         if(!this.flag)  //flag为false,则线程等待进入线程池。  
    17.             try{this.wait();}catch(Exception e){}   
    18.         System.out.println(this.name+"----"+this.sex); //flag为true则继续执行  
    19.         this.flag = false;  
    20.         this.notify();  //任意唤醒线程池中一个等待的线程  
    21.     }  
    22. }  
    23. class Input implements Runnable{  
    24.     private Res r;  
    25.     Input(Res r){  
    26.         this.r = r;  
    27.     }  
    28.     public void run(){  
    29.         int x = 0;  
    30.         while(true){  
    31.             if(x == 0)  
    32.                 r.setRes("LuoQi","man");  
    33.             else  
    34.                 r.setRes("丽丽","女");  
    35.             x = (x+1)%2;      
    36.         }  
    37.     }  
    38. }  
    39. class Output implements Runnable{  
    40.     private Res r;  
    41.     Output(Res r){  
    42.         this.r = r;  
    43.     }  
    44.     public void run(){  
    45.         while(true){  
    46.             r.getRes();  
    47.         }  
    48.     }  
    49. }  
    50. class ThreadCommunication2{  
    51.     public static void main(String[] args){  
    52.         Res r = new Res();  
    53.           
    54.         Input in = new Input(r);  
    55.         Output out = new Output(r);  
    56.           
    57.         Thread t1 = new Thread(in);  
    58.         Thread t2 = new Thread(out);  
    59.         t1.start();  
    60.         t2.start();  
    61.     }  
    62. }  

    2  生产者消费者问题

    2.1  JDK1.5以前

    使用线程间通信和线程同步解决生产者消费者问题。

    while循环判断标记和notifyAll()

    当出现多个生产者和多个消费者时,必须用while循环判断标记,和notifyAll唤醒全部线程。

    对于多个生产者和消费者,为什么要定义while判断标记?

    原因:让被唤醒的线程再一次判断标记。

    为什么使用notifyAll()

    因为需要唤醒对方线程,因为notify是随机唤醒一个线程,容易出现只唤醒本方线程的情况,导致程序中的所有线程都等待。

    代码和注释:

    [java] view plaincopy在CODE上查看代码片派生到我的代码片
     
    1. package mypkg;  
    2. class ProducerConsumerDemo{  
    3.     public static void main(String[] args){  
    4.         Resource r = new Resource();  
    5.           
    6.         Producer pro = new Producer(r);  
    7.         Consumer con = new Consumer(r);  
    8.           
    9.         Thread t1 = new Thread(pro);  //两个生产者线程  
    10.         Thread t2 = new Thread(pro);  
    11.         Thread t3 = new Thread(con);  //两个消费者线程  
    12.         Thread t4 = new Thread(con);  
    13.         t1.start();  
    14.         t2.start();  
    15.         t3.start();  
    16.         t4.start();   
    17.     }  
    18. }  
    19. class Resource{  
    20.     private String name;  
    21.     private int count = 1;  
    22.     private boolean flag = false;  
    23.       
    24.     public synchronized void set(String name){  //t1  t2  
    25.         while(this.flag)           //while循环判断标记,让被唤醒的线程再次判断标记。标记为true则线程等待,为false则线程继续执行  
    26.             try{this.wait();} catch(Exception e){}  
    27.         this.name = name+"--"+count++;  
    28.         System.out.println(Thread.currentThread().getName()+"---生产者---"+this.name);  
    29.         this.flag = true;  
    30.         this.notifyAll();   //必须唤醒对方,索性唤醒全部。因为有可能生产者唤醒了生产者,导致有的商品被生产了但没被消费。  
    31.     }  
    32.     public synchronized void get(){  //t3  t4  
    33.         while(!this.flag)  
    34.             try{this.wait();} catch(Exception e){}            
    35.         System.out.println(Thread.currentThread().getName()+"---消费者---------"+this.name);  
    36.         this.flag = false;  
    37.         this.notifyAll();  
    38.     }  
    39. }  
    40. class Producer implements Runnable{  
    41.     private Resource r;  
    42.     Producer(Resource r){  
    43.         this.r = r;  
    44.     }  
    45.     public void run(){  
    46.         while(true){  
    47.             r.set("+商品+");  
    48.         }  
    49.     }  
    50. }  
    51. class Consumer implements Runnable{  
    52.     private Resource r;  
    53.     Consumer(Resource r){  
    54.         this.r = r;  
    55.     }  
    56.     public void run(){  
    57.         while(true){  
    58.             r.get();  
    59.         }  
    60.     }  
    61. }  

    运行结果:

    2.2  JDK1.5以后

    JDK1.5 中提供了线程同步和线程间通信的升级解决方案,线程同步、线程间通信和等待唤醒机制都有了变化。

    1,将同步Synchronized替换成显式的Lock操作。

    2,将同步锁继承自Object类的wait()notify()notifyAll()操作,替换成了Condition对象的await()signal()signalAll()操作。

    3,该Condition对象可以通过显式的Lock锁来创建。

    显式的锁机制,以及显式的锁对象上的等待唤醒操作机制,同时把等待唤醒进行封装。

    封装完后,一个锁可以对应多个Condition,等待和唤醒必须是同一个Condition对象调用。

    JDK1.5之前,等待和唤醒必须是同一个锁调用;

    JDK1.5之后,等待和唤醒必须是同一个Condition对象调用,而一个Lock锁可以创建多个Condition对象。

    从而,可以在生产者线程中,只唤醒消费者的等待线程,即调用消费者的Condition对象的唤醒操作。

    Lock接口,它的一个子类是ReentrantLock,创建对象时new一个ReentrantLock对象。

    ReentrantLock类的常用方法:

    newCondition():创建锁LockCondition对象,用来调用操作。 

    lock():获取锁。

    unlock():释放此锁。

    Condition类的常用方法:

    await(): 线程进入等待状态,并抛出一个InterruptedException异常。

    signal(): 唤醒一个等待线程。

    signalAll(): 唤醒所有等待线程。

    [java] view plaincopy在CODE上查看代码片派生到我的代码片
     
    1. import java.util.concurrent.locks.*;   
    2. class ProducerConsumerDemo2{  
    3.     public static void main(String[] args){  
    4.         Resource r = new Resource();  
    5.           
    6.         Producer pro = new Producer(r);  
    7.         Consumer con = new Consumer(r);  
    8.           
    9.         Thread t1 = new Thread(pro);  
    10.         Thread t2 = new Thread(pro);  
    11.         Thread t3 = new Thread(con);  
    12.         Thread t4 = new Thread(con);  
    13.         t1.start();  
    14.         t2.start();  
    15.         t3.start();  
    16.         t4.start();   
    17.     }  
    18. }  
    19. class Resource{  
    20.     private String name;  
    21.     private int count = 1;  
    22.     private boolean flag = false;  
    23.       
    24.     final Lock lock = new ReentrantLock();  //创建一个锁  
    25.     final Condition condition_pro = lock.newCondition(); //创建锁lock的Condition对象,用来操作生产者线程  
    26.     final Condition condition_con = lock.newCondition(); //创建锁lock的Condition对象,用来操作消费者线程  
    27.       
    28.     public void set(String name) throws InterruptedException {  //t1  t2  
    29.         lock.lock();  
    30.         try{  
    31.             while(this.flag)            
    32.                 condition_pro.await(); //await():线程等待,会抛出一个异常  
    33.             this.name = name+"--"+count++;  
    34.             System.out.println(Thread.currentThread().getName()+"---生产者---"+this.name);  
    35.             this.flag = true;  
    36.             condition_con.signal();   //生产者中唤醒消费者  
    37.         }  
    38.         finally{  
    39.             lock.unlock();  //释放锁的动作一定要执行,所以在finally中  
    40.         }  
    41.     }  
    42.     public void get() throws InterruptedException {  //t3  t4  
    43.         lock.lock();  
    44.         try{  
    45.             while(!this.flag)  
    46.                 condition_con.await();        
    47.             System.out.println(Thread.currentThread().getName()+"---消费者---------"+this.name);  
    48.             this.flag = false;  
    49.             condition_pro.signal();  //消费者中唤醒生产者  
    50.         }  
    51.         finally{  
    52.             lock.unlock();  
    53.         }  
    54.     }  
    55. }  
    56. class Producer implements Runnable{  
    57.     private Resource r;  
    58.     Producer(Resource r){  
    59.         this.r = r;  
    60.     }  
    61.     public void run(){  
    62.         while(true){  
    63.             try{  
    64.                 r.set("+商品+");  
    65.             }  
    66.             catch(InterruptedException e){}  
    67.         }  
    68.     }  
    69. }  
    70. class Consumer implements Runnable{  
    71.     private Resource r;  
    72.     Consumer(Resource r){  
    73.         this.r = r;  
    74.     }  
    75.     public void run(){  
    76.         while(true){  
    77.             try{  
    78.                 r.get();  
    79.             }  
    80.             catch(InterruptedException e){}  
    81.         }   
    82.     }  
    83. }  

    3  停止线程和守护线程

    3.1  停止线程

    以前可以使用stop方法来停止线程,但是已经过时,那现在如何停止线程?

    只有一种方法:run方法结束。

    开启多线程运行,run方法内的运行代码通常是循环结构,

    只要控制住循环,就可以让run方法结束,也就是线程结束。

    特殊情况:

    当线程处于了冻结状态,就不会读取到标记,那么线程就不会结束。

    当没有指定的方式让冻结的线程恢复到运行状态时,这是需要对冻结进行清除。

    强制让现场恢复到运行状态中来,这样就可以操作标记让线程结束。

    Thread类中提供该方法,interrupt()方法。

    interrupt()方法是把线程从冻结状态恢复到运行状态。

    3.2  守护线程

    Thread类中的setDaemon方法

    setDaemon(boolean on)

    on如果为 true,则将该线程标记为守护线程。

    守护线程,当正在运行的线程都是守护线程时,Java 虚拟机退出。该方法必须在启动线程前调用。

    JVM退出,守护线程在后台执行,理解为后台线程;全部为后台线程时,由前台转为后台,JVM则退出。

    代码示例:

    [java] view plaincopy在CODE上查看代码片派生到我的代码片
     
    1. class StopThread implements Runnable{  
    2.     private boolean flag = true;  
    3.     public synchronized void run(){  
    4.         while(flag){  
    5.             try{  
    6.                 wait();  
    7.             }  
    8.             catch(InterruptedException e){  
    9.                 System.out.println(Thread.currentThread().getName()+"....Exception");  
    10.                 flag = false;  
    11.             }  
    12.             System.out.println(Thread.currentThread().getName()+"....run");  
    13.         }  
    14.     }  
    15.     public void changeFlag(){  
    16.         flag = false;  
    17.     }   
    18. }  
    19. class StopThreadDemo{  
    20.     public static void main(String[] args){  
    21.         StopThread st = new StopThread();  
    22.           
    23.         Thread t1 = new Thread(st);  
    24.         Thread t2 = new Thread(st);  
    25.         //t1.setDaemon(true);  //守护线程,当正在运行的线程都是守护线程时,Java 虚拟机退出。该方法必须在启动线程前调用。  
    26.         //t2.setDaemon(true);  
    27.         t1.start();  
    28.         t2.start();  
    29.           
    30.         int num = 0;  
    31.         while(true){  
    32.             if(num++ == 60){  
    33.                 //st.changeFlag();  
    34.                 t1.interrupt();  //中断线程,让线程从冻结状态恢复到运行状态,这样可以读到flag标记从而结束线程。  
    35.                 t2.interrupt();  
    36.                 break;  //跳出while循环  
    37.             }  
    38.             System.out.println(Thread.currentThread().getName()+"......"+num);  
    39.         }  
    40.         System.out.println("over");  
    41.     }  
    42. }  

    4  线程的join()方法

    join()

    A线程执行到了B线程的join()方法时,那么A线程就会等待;等B线程执行完,A才会执行。

    join()可以用来临时加入线程执行。

    代码示例:

    [java] view plaincopy在CODE上查看代码片派生到我的代码片
     
    1. class Demo implements Runnable{  
    2.     public void run(){  
    3.         for(int x=0;x<70;x++){  
    4.             System.out.println(Thread.currentThread().getName()+"....."+x);  
    5.         }  
    6.     }  
    7. }  
    8. class JoinDemo{  
    9.     public static void main(String[] args) throws Exception{  
    10.         Demo d = new Demo();  
    11.         Thread t1 = new Thread(d);  
    12.         Thread t2 = new Thread(d);  
    13.         t1.start();  
    14.         t2.start();  
    15.         t1.join();  //t1线程向主线程索要CPU执行权,主线程阻塞,释放CPU执行权,但释放后t1和t2竞争CPU执行权;  
    16.                      //t1线程执行结束后,主线程继续。  
    17.           
    18.         for(int x=0; x<80; x++){  
    19.             System.out.println("main...."+x);  
    20.         }  
    21.         System.out.println("over");  
    22.     }  
    23. }  

    5  线程优先级和yield()方法

    5.1  线程优先级

    线程优先级:

    优先级高的线程,争夺CPU执行权的频率就高,拿到CPU资源的可能性更大,

    但并不是说优先级低的线程就不执行了。

    Thread类中定义了三个优先级常量:

    MAX_PRIORITY 值为10,为最高优先级;

    MIN_PRIORITY 值为1,为最低优先级;

    NORM_PRIORITY 值为5,默认优先级。

    新建线程将继承创建它的父线程的优先级,父线程是指执行创建新线程的语句所在线程,它可能是主线程,也可能是另一个自定义线程。

    一般情况下,主线程具有默认优先级,为5

    可以通过getPriority()方法获得线程的优先级,也可以通过setPriority()方法来设定优先级。

    5.2  yield()方法

    yield()方法:调用该方法后,可以使具有与当前线程相同优先级的线程有运行的机会。

    可以临时暂停当前线程,释放CPU执行权,让相同优先级的其他线程运行。

    如果没有相同优先级的线程,那么yield()方法什么也不做,当前线程继续运行。

    代码示例:

    [java] view plaincopy在CODE上查看代码片派生到我的代码片
     
    1. class Demo implements Runnable{  
    2.     public void run(){  
    3.         for(int x=0;x<70;x++){  
    4.             System.out.println(Thread.currentThread().getName()+"....."+x);  
    5.             Thread.yield(); //t1暂停,t2运行;t2暂停,t1运行。  
    6.                              //表现为t1、t2交替执行。  
    7.         }  
    8.     }  
    9. }  
    10. class YieldDemo{  
    11.     public static void main(String[] args){  
    12.         Demo d = new Demo();  
    13.         Thread t1 = new Thread(d);  
    14.         Thread t2 = new Thread(d);  
    15.         t1.start();  
    16.         t2.start();  
    17.           
    18.         for(int x=0; x<80; x++){  
    19.             //System.out.println("main...."+x);  
    20.         }  
    21.         System.out.println("over");  
    22.     }  
    23. }  

    6  开发中什么时候使用多线程?

    当某些代码需要同时被执行时,就用单独的线程进行封装。

    比如三个for循环同时运行,用多线程,高效,代码示例:

    [java] view plaincopy在CODE上查看代码片派生到我的代码片
     
      1. class ThreadTest{   //三个for同时运行,用多线程,高效。  
      2.     public static void main(String[] args){  
      3.         new Thread(){  //匿名内部类  
      4.             public void run(){  
      5.                 for(int x=0; x<50; x++){  
      6.                     System.out.println(Thread.currentThread().getName()+"....."+x);  
      7.                 }  
      8.             }  
      9.         }.start();  
      10.           
      11.         for(int x=0; x<50; x++){  
      12.             System.out.println(Thread.currentThread().getName()+"....."+x);  
      13.         }  
      14.           
      15.         Runnable r = new Runnable(){   //匿名内部类  
      16.             public void run(){  
      17.                 for(int x=0; x<50; x++){  
      18.                     System.out.println(Thread.currentThread().getName()+"....."+x);  
      19.                 }         
      20.             }  
      21.         };  
      22.         new Thread(r).start();  
      23.     }  
      24. }  
  • 相关阅读:
    快速排序模板
    C++面试题目汇总
    二叉树两个节点的最大路径
    vim中操作的快捷键
    剑指offer 顺时针打印矩阵
    剑指offer 判断一棵树是不是另一棵树的子结构
    poi导出excel表
    java 获取服务器(ftp)指定文件夹内的文件
    部署web应用
    tomcat 的目录结构及修改端口号
  • 原文地址:https://www.cnblogs.com/wliangde/p/3699798.html
Copyright © 2011-2022 走看看