zoukankan      html  css  js  c++  java
  • Java多线程2线程协作、Timer和TimerTask

           线程协作

           生产者/消费者模式是一个经典的线程同步以及通信的模型。

            假设有这样一种情况,有一个盘子,盘子里只能放一个鸡蛋,A线程专门往盘子里放鸡蛋,如果盘子里有鸡蛋,则一直等到盘子里没鸡蛋,B线程专门从盘子里取鸡蛋,如果盘子里没鸡蛋,则一直等到盘子里有鸡蛋。这里盘子是一个互斥区,每次放鸡蛋是互斥的,每次取鸡蛋也是互斥的,A线程放鸡蛋,如果这时B线程要取鸡蛋,由于A没有释放锁,B线程处于等待状态,进入阻塞队列,放鸡蛋之后,要通知B线程取鸡蛋,B线程进入就绪队列,反过来,B线程取鸡蛋,如果A线程要放鸡蛋,由于B线程没有释放锁,A线程处于等待状态,进入阻塞队列,取鸡蛋之后,要通知A线程放鸡蛋,A线程进入就绪队列。我们希望当盘子里有鸡蛋时,A线程阻塞,B线程就绪,盘子里没鸡蛋时,A线程就绪,B线程阻塞,代码如下:

    1. import java.util.ArrayList;  
    2. import java.util.List;  
    3. /** 定义一个盘子类,可以放鸡蛋和取鸡蛋 */  
    4. public class Plate {  
    5.     /** 装鸡蛋的盘子 */  
    6.     List<Object> eggs = new ArrayList<Object>();  
    7.     /** 取鸡蛋 */  
    8.     public synchronized Object getEgg() {  
    9.         while (eggs.size() == 0) {  
    10.             try {  
    11.                 wait();  
    12.             } catch (InterruptedException e) {  
    13.                 e.printStackTrace();  
    14.             }  
    15.         }  
    16.         Object egg = eggs.get(0);  
    17.         eggs.clear();// 清空盘子  
    18.         notify();// 唤醒阻塞队列的某线程到就绪队列  
    19.         System.out.println("拿到鸡蛋");  
    20.         return egg;  
    21.     }  
    22.     /** 放鸡蛋 */  
    23.     public synchronized void putEgg(Object egg) {  
    24.         while (eggs.size() > 0) {  
    25.             try {  
    26.                 wait();  
    27.             } catch (InterruptedException e) {  
    28.                 e.printStackTrace();  
    29.             }  
    30.         }  
    31.         eggs.add(egg);// 往盘子里放鸡蛋  
    32.         notify();// 唤醒阻塞队列的某线程到就绪队列  
    33.         System.out.println("放入鸡蛋");  
    34.     }  
    35.     static class AddThread extends Thread {  
    36.         private Plate plate;  
    37.         private Object egg = new Object();  
    38.         public AddThread(Plate plate) {  
    39.             this.plate = plate;  
    40.         }  
    41.         public void run() {  
    42.             plate.putEgg(egg);  
    43.         }  
    44.     }  
    45.     static class GetThread extends Thread {  
    46.         private Plate plate;  
    47.         public GetThread(Plate plate) {  
    48.             this.plate = plate;  
    49.         }  
    50.         public void run() {  
    51.             plate.getEgg();  
    52.         }  
    53.     }  
    54.     public static void main(String args[]) {  
    55.         Plate plate = new Plate();  
    56.         for(int i = 0; i < 10; i++) {  
    57.             new Thread(new AddThread(plate)).start();  
    58.             new Thread(new GetThread(plate)).start();  
    59.         }  
    60.     }  
    61. }  
            输出结果:

    1. 放入鸡蛋  
    2. 拿到鸡蛋  
    3. 放入鸡蛋  
    4. 拿到鸡蛋  
    5. 放入鸡蛋  
    6. 拿到鸡蛋  
    7. 放入鸡蛋  
    8. 拿到鸡蛋  
    9. 放入鸡蛋  
    10. 拿到鸡蛋  
    11. 放入鸡蛋  
    12. 拿到鸡蛋  
    13. 放入鸡蛋  
    14. 拿到鸡蛋  
    15. 放入鸡蛋  
    16. 拿到鸡蛋  
    17. 放入鸡蛋  
    18. 拿到鸡蛋  
    19. 放入鸡蛋  
    20. 拿到鸡蛋  
            8 l程序开始,A线程判断盘子是否为空,放入一个鸡蛋,并且唤醒在阻塞队列的一个线程,阻塞队列为空;假设CPU又调度了一个A线程,盘子非空,执行等待,这个A线程进入阻塞队列;然后一个B线程执行,盘子非空,取走鸡蛋,并唤醒阻塞队列的A线程,A线程进入就绪队列,此时就绪队列就一个A线程,马上执行,放入鸡蛋;如果再来A线程重复第一步,在来B线程重复第二步,整个过程就是生产者(A线程)生产鸡蛋,消费者(B线程)消费鸡蛋。

            下面讲述了一个线程通信的,在此也分享一下。

            题目:子线程循环10次,主线程循环100次,如此循环100次。

    1. public class ThreadTest2 {  
    2.     public static void main(String[] args) {  
    3.         final Business business = new Business();  
    4.         new Thread(new Runnable() {  
    5.             @Override  
    6.             public void run() {  
    7.                 threadExecute(business, "sub");  
    8.             }  
    9.         }).start();  
    10.         threadExecute(business, "main");  
    11.     }     
    12.     public static void threadExecute(Business business, String threadType) {  
    13.         for(int i = 0; i < 100; i++) {  
    14.             try {  
    15.                 if("main".equals(threadType)) {  
    16.                     business.main(i);  
    17.                 } else {  
    18.                     business.sub(i);  
    19.                 }  
    20.             } catch (InterruptedException e) {  
    21.                 e.printStackTrace();  
    22.             }  
    23.         }  
    24.     }  
    25. }  
    26. class Business {  
    27.     private boolean bool = true;  
    28.     public synchronized void main(int loop) throws InterruptedException {  
    29.         while(bool) {  
    30.             this.wait();  
    31.         }  
    32.         for(int i = 0; i < 100; i++) {  
    33.             System.out.println("main thread seq of " + i + ", loop of " + loop);  
    34.         }  
    35.         bool = true;  
    36.         this.notify();  
    37.     }     
    38.     public synchronized void sub(int loop) throws InterruptedException {  
    39.         while(!bool) {  
    40.             this.wait();  
    41.         }  
    42.         for(int i = 0; i < 10; i++) {  
    43.             System.out.println("sub thread seq of " + i + ", loop of " + loop);  
    44.         }  
    45.         bool = false;  
    46.         this.notify();  
    47.     }  
    48. }  

            大家注意到没有,在调用wait方法时,都是用while判断条件的,而不是if,在wait方法说明中,也推荐使用while,因为在某些特定的情况下,线程有可能被假唤醒,使用while会循环检测更稳妥。

           Timer和TimerTask可以做为实现线程的第三种方式,前两中方式分别是继承自Thread类和实现Runnable接口。

            Timer是一种线程设施,用于安排以后在后台线程中执行的任务。可安排任务执行一次,或者定期重复执行,可以看成一个定时器,可以调度TimerTask。TimerTask是一个抽象类,实现了Runnable接口,所以具备了多线程的能力。

            一个Timer可以调度任意多个TimerTask,它会将TimerTask存储在一个队列中,顺序调度,如果想两个TimerTask并发执行,则需要创建两个Timer。下面来看一个简单的例子:        

    1. import java.util.Timer;  
    2. import java.util.TimerTask;  
    3. public class TimerTest {  
    4.     static class MyTimerTask1 extends TimerTask {  
    5.         public void run() {  
    6.             System.out.println("爆炸!!!");  
    7.         }  
    8.     }     
    9.     public static void main(String[] args) {  
    10.         Timer timer = new Timer();  
    11.         timer.schedule(new MyTimerTask1(), 2000);// 两秒后启动任务  
    12.     }  
    13. }  

            schedule是Timer调度任务的方法,Timer重构了四个schedule方法,具体可以查看JDK API。

            看一个稍复杂的例子,假设有这样一种需求,实现一个连环炸弹,2秒后爆炸一次,3秒后爆炸一次,如此循环下去,这就需要创建两个任务,互相调度,代码如下:

    1. import java.util.Date;  
    2. import java.util.Timer;  
    3. import java.util.TimerTask;  
    4. public class TimerTest {  
    5.     static class MyTimerTask1 extends TimerTask {  
    6.         public void run() {  
    7.             System.out.println("爆炸!!!");  
    8.             new Timer().schedule(new MyTimerTask2(), 2000);  
    9.         }  
    10.     }  
    11.     static class MyTimerTask2 extends TimerTask {  
    12.         public void run() {  
    13.             System.out.println("爆炸!!!");  
    14.             new Timer().schedule(new MyTimerTask1(), 3000);  
    15.         }  
    16.     }  
    17.     public static void main(String[] args) {  
    18.         Timer timer = new Timer();  
    19.         timer.schedule(new MyTimerTask2(), 2000);  
    20.         while(true) {  
    21.             System.out.println(new Date().getSeconds());  
    22.             try {  
    23.                 Thread.sleep(1000);  
    24.             } catch (InterruptedException e) {  
    25.                 // TODO Auto-generated catch block  
    26.                 e.printStackTrace();  
    27.             }  
    28.         }  
    29.     }  
    30. }  
  • 相关阅读:
    定义和使用EL函数
    在Java Web程序中使用Hibernate
    JDBC在Java Web中的应用——分页查询
    JDBC调用存储过程
    使用navicat工具创建MySQL存储过程
    JDBC操作数据库的批处理
    JDBC操作数据库
    Servlet监听器统计在线人数
    Servlet字符编码过滤器
    Servlet过滤器创建与配置
  • 原文地址:https://www.cnblogs.com/wuyida/p/6301132.html
Copyright © 2011-2022 走看看