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

📄 thinkershell.java

📁 ParallelConnectFour是一个java小游戏
💻 JAVA
字号:
package edu.rit.cs.mlr5773.connectfour;import java.io.*;import java.util.*;/** * A Stub for a Thinker Process.  There is a local ThinkerShell thread * that monitors each Thinker process. * * @author Mark Roth */public class ThinkerShell extends Thread {  /** The input stream to communicate with the Thinker */  private LineNumberReader in;  /** The output stream to communicate with the Thinker */  private PrintWriter out;  /** The name of the host this Thinker is running on */  private String hostName;  /** The id of this Thinker */  private int id;  /** Is this thinker currently awake (i.e. is it processing a request? */  private boolean awake;  /** True if this ThinkerShell is to shut down the Thinker */  private boolean shutdown;  /** A queue of current jobs for this Thinker */  private Vector currentJobs;  /** A queue of current results from the given jobs */  private Vector results;  /**   * Creates a new Thinker Shell to communicate with a Thinker process.   * Establishes a connection with the Thinker, gets it's hostname,   * and sends it number of moves to look ahead.   */  public ThinkerShell( int id, int maxDepth, LineNumberReader in,     PrintWriter out )   {    this.in = in;    this.out = out;    this.id = id;    this.awake = false;    this.shutdown = false;    this.currentJobs = new Vector();    this.results = new Vector();    try {      hostName = in.readLine();      out.println( "" + maxDepth );    }    catch( IOException e ) {      System.err.println( "Error communicating with thinker.  Reason: " + 	e.getMessage() );      System.exit( 0 );    }  }          /**   * Takes the job at the front of the queue and sends it to the Thinker.   * Waits for a response from the Thinker and then places the response   * in the results queue.  Finally, removes the job from the queue.   */  private void doJob() {    ThinkerRequest currentJob = (ThinkerRequest) currentJobs.elementAt(0);    currentJobs.removeElementAt(0);     out.println(currentJob.encode());    String value;    try {      value = in.readLine();    }    catch( java.io.IOException e ) {      System.err.println( "Error receiving value from a node " + id + "." );      value = "0";    }    results.addElement( value );  }  /**   * Retrieves the host name this Thinker is processing on   */  public String getHostName() {    return hostName;  }   /**   * Starts the queue monitor on this Thread.  Constantly monitors the   * queue and submits jobs to the Thinker processes accordingly.   */  public void run() {    System.out.println("Thinker " + id + " running...");    do {      try {        Thread.sleep(100);      }      catch (InterruptedException e) {	break;      }      if (shutdown) break;      if (awake) {	while (currentJobs.size() > 0) {	  doJob();	}	awake = false;      }    } while (true);    // Tell Thinker to Shut down    out.println("SHUTDOWN");        System.out.println("Thinker " + id + " exiting...");  }  /**   * Indicates that this node should shut down now.   */  public void shutdown() {  }  /**   * Submits the given request to the Thinker process   */  public void submitJob( ThinkerRequest request ) {    awake = true;    currentJobs.addElement( request );  }      /**   * Client call to retrieve the next result in the queue, or wait for a   * result to appear   */  public int waitValue() {    String value;    while( results.size() == 0 ) {      try {	Thread.sleep(100);      }      catch (InterruptedException e) {	break;      }    }    value = (String)results.elementAt( 0 );    results.removeElementAt( 0 );	    return new Integer(value).intValue();  }}

⌨️ 快捷键说明

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