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

📄 client.java

📁 Java学习源代码检索系统免费版
💻 JAVA
字号:
//==============================================================
// Client.java - Class that feeds jobs to a server for processing
//
// Java学习源代码检索系统 Ver 1.0 20031015 免费正式版
// 版权所有: 中国IT认证实验室(www.ChinaITLab.com)
// 程序制作: ChinaITLab网校教研中心
// 主页地址: www.ChinaITLab.com    中国IT认证实验室
// 论坛地址: bbs.chinaitlab.com  
// 电子邮件: Java@ChinaITLab.com
//==============================================================

import java.util.Random;
import Job;
import Server;

class Client implements Runnable {

 Random rand = new Random();
 boolean finished = false;
 int jobcount = 0;

// Utility method creates a new numbered job with
// a random time delay to simulate how long the job takes
 private Job getAJob() {
  String name = "Job #" + ++jobcount;
  int delay = 1000 + rand.nextInt(9000);  // 1 .. 10 seconds
  Job j = new Job(name, delay);
  return j;
 }

 public void run() {

// Create the server daemon thread
  Server server = new Server();
  Thread T = new Thread(server);
  T.setDaemon(true);  // Server is a daemon!
  T.start();          // Start the server thread running

// Main run() actions
  try {
   while (!finished) {

   // Create a job and pass it to the server
    Job j = getAJob();  // Create simulated job object
    server.add(j);      // Returns immediately

   // Simulate user activity by sleeping a random time
    int time = 1000 + rand.nextInt(5000);
    System.out.println("Sleeping for " + 
     time / 1000 + " second(s)");
    Thread.currentThread().sleep(time);

   }
  } catch (InterruptedException e) {
   return;
  }
 }

// Halt the client
// However, all job threads finish to completion!
 public synchronized void halt() {
  finished = true;
  notifyAll();
 }
}

⌨️ 快捷键说明

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