⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 job.java

📁 Java学习源代码检索系统免费版
💻 JAVA
字号:
//==============================================================
// Job.java - A Runnable class that does its own job in a thread
//
// Java学习源代码检索系统 Ver 1.0 20031015 免费正式版
// 版权所有: 中国IT认证实验室(www.ChinaITLab.com)
// 程序制作: ChinaITLab网校教研中心
// 主页地址: www.ChinaITLab.com    中国IT认证实验室
// 论坛地址: bbs.chinaitlab.com  
// 电子邮件: Java@ChinaITLab.com
//==============================================================

public class Job implements Runnable {

 private String name;    // Name of this job
 private int delay;      // How long it takes to do this job
 private boolean ready;  // True when job is ready to be done

// Constructor
 Job(String name, int delay) {
  this.name = name;
  this.delay = delay;
  ready = false;
  new Thread(this).start();  // Job runs itself in a thread!
 }

// Run method called by thread scheduler
 public void run() {
  try {
   doWhenReady();  // Do the job when it is ready
  } catch (InterruptedException e) {
   return;
  }
 }

// Performs the job's actual work
// Because this calls wait(), code cannot be in run()
// and it must be synchronized on this object
 private synchronized void doWhenReady()
  throws InterruptedException {
  while (!ready)
   wait();        // Wait indefinitely until ready
// Simulate the job by displaying messages and sleeping
// for the amount of time this job takes
  System.out.println("\nStarting " + name);
  System.out.println("Time = " + delay / 1000 + " second(s)");
  Thread.currentThread().sleep(delay);  // Simulate job runtime
  System.out.println("\nFinishing " + name);
  System.out.println("Ending thread " + toString());
 }

// Set the thread state flag to true 
// and notify all threads of the change
 public synchronized void doJob() {
  ready = true;
  notifyAll();
 }
}

⌨️ 快捷键说明

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