Don't like ads? PRO users don't see any ads ;-)
Guest

Alarm Class waitUntil()

By: a guest on May 1st, 2012  |  syntax: Java  |  size: 1.62 KB  |  hits: 22  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. /*
  2.      * Tests the functionality of the alarm class.
  3.      *  Because "nachos will not function correctly with more than one alarm",
  4.      *  and the only alarm is instantiated in the threadKernel class, the
  5.      *  alarm is passed here from the threadKernel's selfTest.
  6.      */
  7.     public static void selfTest(final Alarm alarm) {
  8.         System.out.format("\nTesting waitUntil in the Alarm class...\n");
  9.         KThread alarmThread1 = new KThread(new Runnable() {
  10.                 public void run(){
  11.                         System.out.format("Thread 1 waiting, 500000 ticks...\n");
  12.                         alarm.waitUntil(500000);
  13.                         System.out.format("\nThread 1 done waiting.\n\n");
  14.                 }
  15.         });
  16.         KThread alarmThread2 = new KThread(new Runnable() {
  17.                 public void run(){
  18.                         System.out.format("Thread 2 waiting, 1000000 ticks...\n");
  19.                         alarm.waitUntil(1000000);
  20.                         System.out.format("Thread 2 done waiting.\n\n");
  21.                 }
  22.         });
  23.         KThread alarmThread3 = new KThread(new Runnable() {
  24.                 public void run(){
  25.                         System.out.format("Thread 3 waiting, 3000000 ticks...\n");
  26.                         alarm.waitUntil(3000000);
  27.                         System.out.format("Thread 3 done waiting.\n\n");
  28.                 }
  29.         });
  30.         KThread alarmThread4 = new KThread(new Runnable() {
  31.                 public void run(){
  32.                         System.out.format("Thread 4 waiting, 5000000 ticks...\n");
  33.                         alarm.waitUntil(5000000);
  34.                         System.out.format("Thread 4 done waiting.\n\n");
  35.                 }
  36.         });
  37.        
  38.         alarmThread1.fork();
  39.         alarmThread2.fork();
  40.         alarmThread3.fork();
  41.         alarmThread4.fork();
  42.         // have to join or machine halts prematurely
  43.         alarmThread4.join();
  44.     }