1 package test; 2 3 import java.io.*; 4 5 public class Test5 { 6 7 private static boolean _stop = false; 8 9 public static void main(String args[]) throws Exception { 10 int threads = 10; 11 12 for (int i = 0; i < threads; i++) { 13 Thread t = new Thread (new Runnable () { 14 public void run() { 15 int i = 0; 16 while (!_stop) { 17 i++; 18 try { 19 Thread.sleep(10); 20 } catch (InterruptedException e) { 21 e.printStackTrace(); 22 } 23 } 24 System.out.println("Thread " + Thread.currentThread().getName() + " terminating"); 25 } 26 }); 27 t.setDaemon(true); 28 t.setName("t" + i); 29 t.start(); 30 } 31 32 System.out.println("Started all threads"); 33 BufferedReader kbd = new BufferedReader(new InputStreamReader(System.in)); 34 kbd.readLine(); 35 System.out.println("Shutting down"); 36 _stop = true; 37 } 38 } | Popular Tags |