1 8 9 package mx4j.examples.mbeans.legacy; 10 11 20 public class LegacyService 21 { 22 private boolean running; 23 private ThreadGroup group = new ThreadGroup ("Legacy Thread Group"); 24 25 29 public void execute() 30 { 31 while (true) 32 { 33 long wait = Math.round(Math.random() * 10000L) + 1; 35 try 36 { 37 System.out.println("Waiting " + wait + " ms..."); 38 Thread.sleep(wait); 39 } 40 catch (InterruptedException ignored) 41 { 42 } 43 synchronized (this) 45 { 46 running = true; 47 } 48 49 Thread thread = new Thread (new Runnable () 50 { 51 public void run() 52 { 53 spawnThreads(); 54 synchronized (this) 56 { 57 running = false; 58 } 59 } 60 }); 61 thread.start(); 62 try 63 { 64 thread.join(); 65 } 66 catch (InterruptedException ignored) 67 { 68 } 69 } 70 } 71 72 77 private synchronized boolean isRunning() 78 { 79 return running; 80 } 81 82 private void spawnThreads() 83 { 84 Thread [] threads = new Thread [20]; 85 for (int i = 0; i < threads.length; ++i) 86 { 87 threads[i] = new Thread (group, new Runnable () 88 { 89 public void run() 90 { 91 long sleep = Math.round(Math.random() * 5000L) + 1; 93 try 94 { 95 Thread.sleep(sleep); 96 } 97 catch (InterruptedException ignored) 98 { 99 } 100 } 101 }); 102 threads[i].start(); 103 } 104 105 for (int i = 0; i < threads.length; ++i) 107 { 108 try 109 { 110 threads[i].join(); 111 } 112 catch (InterruptedException ignored) 113 { 114 } 115 } 116 } 117 } 118 | Popular Tags |