clockapplet.java

来自「这是本人开发的一个小时钟 主要涉及到java的多线程技术」· Java 代码 · 共 69 行

JAVA
69
字号
public class ClockApplet extends CurrentTimeApplet implements Runnable
{
 //通过封装线程对象来创建线程
 private Thread thread=null;
 private boolean suspended=false;
 
 public void init()
  {
     super.init();
     thread=new Thread(this);
     thread.start();//与applet中的start()方法不一样
  }

//applet中的start()方法 让中的方法与线程的方法相结合
 public void start()
  {
   resume();
  }
 public void run()
  {
    while(true)
     {
        stillClock.repaint();
         
        try
          {
            thread.sleep(1000);
            waitForNotificationToResume();
          }
        catch(InterruptedException ex)
          {}
     }
  }

//处理挂起操作 原来的suspended方法禁用
private synchronized void waitForNotificationToResume() throws InterruptedException
{
  while(suspended)
       wait();
}

//applet 中的stop()方法
public void stop()
{
  suspend();
}

//applet 中的destroy()方法
public void destroy()
{
  thread=null;
}

//处理resume(唤醒)操作 原来的被禁止使用
public synchronized void resume()
{
  if(suspended)
     {
       suspended=false;
       notify();
     }
}

public synchronized void suspend()
{
 suspended=true;
}   

}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?