home *** CD-ROM | disk | FTP | other *** search
/ Java Developer's Companion / Java Developer's Companion.iso / documentation / tutorial / java / threads / example-1.1 / Philosopher.java < prev    next >
Encoding:
Java Source  |  1997-07-13  |  3.7 KB  |  103 lines

  1. /*
  2.  * Copyright (c) 1995-1997 Sun Microsystems, Inc. All Rights Reserved.
  3.  *
  4.  * Permission to use, copy, modify, and distribute this software
  5.  * and its documentation for NON-COMMERCIAL purposes and without
  6.  * fee is hereby granted provided that this copyright notice
  7.  * appears in all copies. Please refer to the file "copyright.html"
  8.  * for further important copyright and licensing information.
  9.  *
  10.  * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF
  11.  * THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
  12.  * TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
  13.  * PARTICULAR PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR
  14.  * ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
  15.  * DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES.
  16.  */
  17.  
  18. import java.awt.*;
  19.  
  20. class Philosopher extends Thread {
  21.     Chopstick leftStick, rightStick;
  22.     double x, y;
  23.     boolean sated;
  24.     PhilosopherArea parent;
  25.     int position;
  26.     boolean stopRequested = false;
  27.  
  28.     Philosopher(PhilosopherArea parent, double x, double y, int position) {
  29.         super(parent.names[position]);
  30.  
  31.         this.parent = parent;
  32.         this.position = position;
  33.         this.x = x;
  34.         this.y = y;
  35.  
  36.     // identify the chopsticks to my right and left
  37.         this.rightStick = this.parent.chopsticks[position];
  38.         if (position == 0) {
  39.             this.leftStick = this.parent.chopsticks[this.parent.NUMPHILS-1];
  40.         } else {
  41.             this.leftStick = this.parent.chopsticks[position-1];
  42.         }
  43.  
  44.     // I'm hungry
  45.         this.sated = false;
  46.     }
  47.  
  48.     public void run() {
  49.         int grabDelay;
  50.         while (stopRequested == false) {
  51.              try {
  52.                 grabDelay = parent.controller.grabDelaySlider.getValue() * 100;
  53.                 sleep((int)(Math.random() * grabDelay));
  54.                 rightStick.grab(this);
  55.                 parent.repaintPhil(position);
  56.  
  57.                 grabDelay = parent.controller.grabDelaySlider.getValue() * 100;
  58.                 sleep((int)(Math.random() * grabDelay));
  59.                 leftStick.grab(this);
  60.                 parent.repaintPhil(position);
  61.  
  62.                 grabDelay = parent.controller.grabDelaySlider.getValue() * 100;
  63.                 sleep((int)(Math.random() * grabDelay));
  64.                 eat();
  65.  
  66.                 grabDelay = parent.controller.grabDelaySlider.getValue() * 100;
  67.                 sleep((int)(Math.random() * grabDelay * 4));
  68.                 sated = false;
  69.                 parent.repaintPhil(position);
  70.             } catch (java.lang.InterruptedException e) {
  71.             }
  72.         }
  73.     }
  74.  
  75.     public void eat() {
  76.         rightStick.release(this);
  77.         leftStick.release(this);
  78.         sated = true;
  79.         parent.repaintPhil(position);
  80.     }
  81.  
  82.     public void paint(Graphics g) {
  83.         g.setColor(Color.lightGray);
  84.         g.fillRect((int)x, (int)y, parent.imgs[0].getWidth(parent), parent.imgs[0].getHeight(parent)+25);
  85.         if (sated == false) {
  86.             if (rightStick.owner == this && leftStick.owner != this) {                // got left only
  87.                 g.drawImage(parent.imgs[1], (int)x, (int)y, parent);
  88.             } else if (rightStick.owner == this && leftStick.owner == this) {        // got both
  89.                 g.drawImage(parent.imgs[2], (int)x, (int)y, parent);
  90.             } else {                                                                // got nothing
  91.                 g.drawImage(parent.imgs[0], (int)x, (int)y, parent);
  92.             }
  93.         } else {
  94.             g.drawImage(parent.imgs[0], (int)x, (int)y, parent);
  95.             g.setColor(Color.black);
  96.             g.drawString("Mmm!", ((int)(x)+8), ((int)(y)+parent.imgs[0].getHeight(parent)+13));
  97.         }
  98.     }
  99.     public void stopRequested() {
  100.         stopRequested = true;
  101.     }
  102. }
  103.