• 线程运行状态


    1. package base;
    2. public class State implements Runnable {
    3. //当前线程等待1秒或其他线程调用notify或notifyAll()方法
    4. public synchronized void waitFirst() throws InterruptedException {
    5. wait(1000);
    6. System.out.println("我爱你");
    7. }
    8. //当前线程等待永久秒或其他线程调用notify或notifyAll()方法
    9. public synchronized void waitSecond() throws InterruptedException {
    10. wait();
    11. System.out.println(
    12. "I hate you");
    13. }
    14. //唤醒有调用wait() third方法进入等待状态的线程
    15. public synchronized void notifyNow() throws InterruptedException {
    16. notify();
    17. }
    18. @Override
    19. public void run() {
    20. // TODO Auto-generated method stub
    21. try {
    22. waitFirst();
    23. waitSecond();
    24. } catch (InterruptedException e) {
    25. // TODO Auto-generated catch block
    26. e.printStackTrace();
    27. }
    28. }
    29. }
    30. package base;
    31. public class Test {
    32. public static void main(String[] args) throws InterruptedException {
    33. State state=new State();
    34. Thread thread =new Thread(state);
    35. //输出线程状态
    36. System.out.println("新建"+thread.getState());
    37. thread.start();
    38. System.out.println("启动"+thread.getState());
    39. thread.sleep(200);//使新线程waitFirst()方法
    40. System.out.println("计时"+thread.getState());
    41. thread.sleep(2000);//使新线程waitSecond()方法
    42. System.out.println("唤醒 "+thread.getState());
    43. state.notifyNow();
    44. System.out.println("等待"+thread.getState());
    45. thread.sleep(4000);//当前线程休眠4秒,使新线程
    46. System.out.println("终止"+thread.getState());
    47. }
    48. }

  • 相关阅读:
    【Linux】:vi 编辑器的使用
    uniApp笔记
    【无标题】
    springcloud的eureka集群只有一个注册的解决办法
    FPGA的电源供电
    centos给用户分配docker权限
    vue api封装
    数据结构与算法 | 二叉树查询原理
    微信浏览器大字体模式 按钮文字居中用line-height 显示异常
    redis的集群
  • 原文地址:https://blog.csdn.net/weixin_44793200/article/details/126347112