Exemplo n.º 1
0
 public static void main(String[] args) throws Exception {
   for (int i = 0; i < 10; i++) {
     Thread daemon = new Thread(new SimpleDaemons());
     daemon.setDaemon(true); // Must call before start()
     daemon.start();
   }
   print("All daemons started");
   TimeUnit.MILLISECONDS.sleep(175);
 }
Exemplo n.º 2
0
 public void run() {
   try {
     while (true) {
       TimeUnit.MILLISECONDS.sleep(100);
       print(Thread.currentThread() + " " + this);
     }
   } catch (InterruptedException e) {
     print("sleep() interrupted");
   }
 }
 public void run() {
   try {
     while (!Thread.interrupted()) {
       Event event = q.take();
       print(event);
       event.run();
     }
   } catch (InterruptedException e) {
     // Acceptable way to exit
   }
   print("Finished Controller");
 }
 public void run() {
   try {
     while (!Thread.interrupted()) {
       car.waitForWaxing();
       printnb("Wax Off! ");
       TimeUnit.MILLISECONDS.sleep(200);
       car.buffed();
     }
   } catch (InterruptedException e) {
     print("Exiting via interrupt");
   }
   print("Ending Wax Off task");
 }
Exemplo n.º 5
0
 public void run() {
   try {
     while (!Thread.interrupted()) {
       // Blocks until next piece of toast is available:
       Toast t = dryQueue.take();
       t.butter();
       print(t);
       butteredQueue.put(t);
     }
   } catch (InterruptedException e) {
     print("Butterer interrupted");
   }
   print("Butterer off");
 }
Exemplo n.º 6
0
 public void run() {
   try {
     while (!Thread.interrupted()) {
       TimeUnit.MILLISECONDS.sleep(100 + rand.nextInt(500));
       // Make toast
       Toast t = new Toast(count++);
       print(t);
       // Insert into queue
       toastQueue.put(t);
     }
   } catch (InterruptedException e) {
     print("Toaster interrupted");
   }
   print("Toaster off");
 }
Exemplo n.º 7
0
 public void run() {
   try {
     while (!Thread.interrupted()) {
       // Blocks until next piece of toast is available:
       Toast t = finishedQueue.take();
       // Verify that the toast is coming in order,
       // and that all pieces are getting jammed:
       if (t.getId() != counter++ || t.getStatus() != Toast.Status.JAMMED) {
         print(">>>> Error: " + t);
         System.exit(1);
       } else print("Chomp! " + t);
     }
   } catch (InterruptedException e) {
     print("Eater interrupted");
   }
   print("Eater off");
 }
Exemplo n.º 8
0
 public void run() {
   int element = rand.nextInt(N_ELEMENTS);
   while (!Thread.interrupted()) {
     for (int i = 0; i < N_GENES; i++) {
       int previous = element - 1;
       if (previous < 0) previous = N_ELEMENTS - 1;
       int next = element + 1;
       if (next >= N_ELEMENTS) next = 0;
       int oldValue = GRID[element][i].get();
       int newValue = oldValue + GRID[previous][i].get() + GRID[next][i].get();
       newValue /= 3;
       if (!GRID[element][i].compareAndSet(oldValue, newValue)) {
         print("Old value changed from " + oldValue);
       }
     }
   }
 }
Exemplo n.º 9
0
 public void run() {
   try {
     while (!Thread.interrupted()) {
       print(this + " " + "thinking");
       pause();
       // Philosopher becomes hungry
       print(this + " " + "grabbing right");
       right.take();
       print(this + " " + "grabbing left");
       left.take();
       print(this + " " + "eating");
       pause();
       right.drop();
       left.drop();
     }
   } catch (InterruptedException e) {
     print(this + " " + "exiting via interrupt");
   }
 }