madiankunsortformdk 2023-05-04 19:26 采纳率: 100%
浏览 19
已结题

关于#java#的问题,如何解决?

课本上课后一题习题。
不会做了,请大家 给个例子参考一下。

创建两个Thread子类,第一个用run()方法用于最开始的启动,并捕获第二个Thread对象的引用,然后调用wait()。第二个类的run()方法几秒后为第一个线程调用notifAll(),使第一个线程打印消息

  • 写回答

1条回答 默认 最新

  • K·cloud 2023-05-04 19:31
    关注

    在这个例子中,我创建了两个Thread子类。第一个类被用于最开始的启动,它捕获第二个Thread对象的引用,并在synchronized代码块中等待第二个线程的通知。第二个类的run()方法等待了5秒后,执行了notifyAll()方法,唤醒了第一个线程。在第一个线程中,等待接收到第二个线程的通知后,输出了"First thread resumed and received notification from second thread"。最后,在main函数中,我启动了两个线程,并等待它们执行完。

    class FirstThread extends Thread {
        private SecondThread secondThread;
        
        public FirstThread(SecondThread secondThread) {
            this.secondThread = secondThread;
        }
        
        public void run() {
            System.out.println("First thread started");
            System.out.println("Wait for 5 seconds");
            
            try {
                Thread.sleep(5000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            
            System.out.println("First thread is waiting for notification from second thread");
            
            synchronized (secondThread) {
                try {
                    secondThread.wait();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
            
            System.out.println("First thread resumed and received notification from second thread");
        }
    }
    
    class SecondThread extends Thread {
        public void run() {
            System.out.println("Second thread started");
            System.out.println("Wait for 5 seconds");
            
            try {
                Thread.sleep(5000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            
            System.out.println("Second thread is sending notification to first thread");
            
            synchronized (this) {
                notifyAll();
            }
        }
    }
    
    public class Main {
        public static void main(String[] args) {
            SecondThread secondThread = new SecondThread();
            FirstThread firstThread = new FirstThread(secondThread);
            
            secondThread.start();
            firstThread.start();
            
            try {
                secondThread.join();
                firstThread.join();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
    

    如果以上回答对您有所帮助,点击一下采纳该答案~谢谢

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

问题事件

  • 系统已结题 5月17日
  • 已采纳回答 5月9日
  • 创建了问题 5月4日

悬赏问题

  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 unity第一人称射击小游戏,有demo,在原脚本的基础上进行修改以达到要求
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line