public static void selfTest() { comm = new Communicator(); KThread t6 = new KThread(new TestListener()).setName("Listener 1"); t6.fork(); KThread t7 = new KThread(new TestListener()).setName("Listener 2"); t7.fork(); KThread t8 = new KThread(new TestListener()).setName("Listener 3"); t8.fork(); KThread t9 = new KThread(new TestListener()).setName("Listener 4"); t9.fork(); KThread t10 = new KThread(new TestListener()).setName("Listener 5"); t10.fork(); KThread t1 = new KThread(new TestSpeaker(123)).setName("Speaker 1"); t1.fork(); KThread t2 = new KThread(new TestSpeaker(456)).setName("Speaker 2"); t2.fork(); KThread t3 = new KThread(new TestSpeaker(789)).setName("Speaker 3"); t3.fork(); KThread t4 = new KThread(new TestSpeaker(101112)).setName("Speaker 4"); t4.fork(); KThread t5 = new KThread(new TestSpeaker(131415)).setName("Speaker 5"); t5.fork(); t1.join(); t2.join(); t3.join(); t4.join(); t5.join(); t6.join(); t7.join(); t8.join(); t9.join(); t10.join(); KThread.yield(); }
public static void selfTest() { final Lock lock = new Lock(); final Condition2 empty = new Condition2(lock); final LinkedList<Integer> list = new LinkedList<>(); KThread consumer = new KThread( new Runnable() { public void run() { lock.acquire(); while (list.isEmpty()) { empty.sleep(); } Lib.assertTrue(list.size() == 5, "List should have 5 values."); while (!list.isEmpty()) { System.out.println("Removed " + list.removeFirst()); } lock.release(); } }); KThread producer = new KThread( new Runnable() { public void run() { lock.acquire(); for (int i = 0; i < 5; i++) { list.add(i); System.out.println("Added " + i); } empty.wake(); lock.release(); } }); consumer.setName("Consumer"); producer.setName("Producer"); consumer.fork(); producer.fork(); consumer.join(); producer.join(); }
/** * Create the idle thread. Whenever there are no threads ready to be run, and * <tt>runNextThread()</tt> is called, it will run the idle thread. The idle thread must never * block, and it will only be allowed to run when all other threads are blocked. * * <p>Note that <tt>ready()</tt> never adds the idle thread to the ready set. */ private static void createIdleThread() { Lib.assertTrue(idleThread == null); idleThread = new KThread( new Runnable() { public void run() { while (true) yield(); } }); idleThread.setName("idle"); Machine.autoGrader().setIdleThread(idleThread); idleThread.fork(); }