Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class MyProcess implements Runnable {
- final int ID;
- final int nextOne; // ID of the next Runnable process
- final Thread[] threadArray; // reference to the array of threads
- MyProcess(int i, Thread[] array) {
- threadArray = array;
- ID = i;
- nextOne = (i + 1) % Test.THREADCOUNT;
- }
- @Override
- public void run() { // when this runs
- while (true) { // forever
- {
- try {
- Thread.sleep(100000); // go to sleep
- } catch (InterruptedException ex) { // but when interrupted
- System.out.println("MyProcess id " + ID + " running"); //output message
- threadArray[nextOne].interrupt(); // then interrupt the next
- }
- }
- }
- }
- }
- public class Test {
- final static int THREADCOUNT = 15;
- public static void main(String args[]) {
- // an array of threads:
- final Thread[] threadArray = new Thread[THREADCOUNT];
- for (int i = 0; i < THREADCOUNT; i++) { // fill array with threads
- // based on the set of processes
- threadArray[i] = new Thread(new MyProcess(i, threadArray));
- }
- for (int i = 0; i < THREADCOUNT; i++) {
- threadArray[i].start(); // start them all
- // they will all go to sleep
- }
- threadArray[0].interrupt(); // wake up the first
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment