线程实现计时器,多线程间通信

本文深入探讨了Java中的Timer类及其在计时任务中的应用,并详细讲解了多线程间的通信机制,包括wait、notify及notifyAll方法的使用,以及ReentrantLock和Condition如何实现更精确的线程控制。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Timer类:计时器

    public class Timer_Test {
        public static void main(String[] args) {
            new Timer().schedule(new Test(),new Date(),2000);
            while(true){
                System.out.println(new Date());
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
        static class Test extends TimerTask{
            @Override
            public void run() {
                System.out.println("时间到了");
            }
        }
    }

  • schedule(TimerTask task, Date firstTime, long period)
    从指定 的时间开始 ,对指定的任务执行重复的 固定延迟执行 。

    三线程间通讯

  • 在同步代码块中,用哪个对象锁,就用哪个对象调用wait方法。

    创建在Main方法中创建三个线程
  • 代码

       public class Demoe {
           public static void main(String[] args) {
               Print p = new Print();
               new Thread() {
                   @Override
                   public void run() {
                       try {
                           while(true){
                               p.print1();
                           }
    
                       } catch (InterruptedException e) {
                           e.printStackTrace();
                       }
                   }
               }.start();
               new Thread() {
                   @Override
                   public void run() {
                       try {
                           while(true){
                               p.print2();
                           }
    
                       } catch (InterruptedException e) {
                           e.printStackTrace();
                       }
                   }
               }.start();
               new Thread() {
                   @Override
                   public void run() {
                       try {
                           while(true){
                               p.print3();
                           }
    
                       } catch (InterruptedException e) {
                           e.printStackTrace();
                       }
                   }
               }.start();
           }
    
           static class Print {                //创建内部类并创建三个成员方法,使用WAIT和NOTIFYALL实现线程等待和唤醒
               static int flag = 1;
    
               public void print1() throws InterruptedException {
                   synchronized (this) {
                       while (flag != 1) {    //使用wait保证判断语句能够执行,如果使用if将会直接唤醒线程而跳过判断阶段。
                           this.wait();
                       }
                       System.out.print("a");
                       System.out.print("b");
                       System.out.println();
                       flag = 2;
                       this.notifyAll();
                   }
               }
    
               public void print2() throws InterruptedException {
                   synchronized (this) {
                       while (flag != 2) {
                           this.wait();
                       }
                       System.out.print("1");
                       System.out.print("2");
                       System.out.println();
                       flag = 3;
                       this.notifyAll();
                   }
               }
    
               public void print3() throws InterruptedException {
                   synchronized (this) {
                       while (flag != 3) {
                           this.wait();
                       }
                       System.out.print("A");
                       System.out.println("B");
                       flag = 1;
                       this.notifyAll();
                   }
               }
    
           }
    
       }
  • 多个线程通信的问题
    • notify()方法是随机唤醒一个线程
    • notifyAll()方法是唤醒所有线程
    • JDK5之前无法唤醒指定的一个线程
    • 如果多个线程之间通信, 需要使用notifyAll()通知所有线程, 用while来反复判断条件
  • sleep方法与wait方法区别
    • sleep方法有参数,时间结束后,自动醒来
      wait可以有参数也可以没有参数,传入就是在参数时间结束时间后等待,不传入直接等待
    • sleep方法在同步函数或同步代码块中,不释放锁
      wait方法在同步函数或者同步代码块中释放锁

      ReentrantLock实现上述功能

  • 可使用if作为判断语句
  • newCondition()
    返回Condition用于这种用途实例Lock实例。

      ReentrantLock r=new ReentrantLock();
      Condition c=r.newCondition();
  • 使用Lock实例开启锁

      r.lock
      if( flag!=1){
          c.await();
      }
  • 使用指定其他锁开启signal

      c.signal();   
  • 关闭Lock;
  •   r.unlock();

转载于:https://www.cnblogs.com/PoetryAndYou/p/11194183.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值