server.java

来自「Java学习源代码检索系统免费版」· Java 代码 · 共 47 行

JAVA
47
字号
//==============================================================
// Server.java - Server class queues and processes jobs using threads
//
// Java学习源代码检索系统 Ver 1.0 20031015 免费正式版
// 版权所有: 中国IT认证实验室(www.ChinaITLab.com)
// 程序制作: ChinaITLab网校教研中心
// 主页地址: www.ChinaITLab.com    中国IT认证实验室
// 论坛地址: bbs.chinaitlab.com  
// 电子邮件: Java@ChinaITLab.com
//==============================================================

import Queue;
import Job;

class Server implements Runnable {

 Queue q = new Queue();  // Construct our Queue object 

 public void run() {
  try {
   doWhenReady();  // Do the server's activities
  } catch (InterruptedException e) {
   return;
  }
 }

// Perform server activities until shutdown
 private synchronized void doWhenReady()
  throws InterruptedException {
  for (;;) {              // Do "forever" loop
   while (q.isEmpty())    // Wait until there is a job in 
    wait();               //  the queue
   Job j = (Job)q.get();  // Get the job
   j.doJob();             // Do the job
  } // for
 }

// Add a new job to the server's queue
// This returns immediately; the job is not performed
// until the server thread detects the queue is no longer
// empty. 
 public synchronized void add(Job j) {
  q.add(j);
  notifyAll();
 }
}

⌨️ 快捷键说明

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