1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146
| package threadtest;
import java.io.FileWriter; import java.io.IOException; import java.io.PrintWriter; import java.lang.Thread.State;
public class ThreadInfo02 { public static void main(String[] args) throws IOException { Thread[] threads = new Thread[10]; Thread.State status[] = new Thread.State[10]; for (int i=0; i<10; i++){ threads[i]=new Thread(new Calculator02(i)); if ((i%2)==0){ threads[i].setPriority(Thread.MAX_PRIORITY); } else { threads[i].setPriority(Thread.MIN_PRIORITY); } threads[i].setName("Thread "+i); } PrintWriter pw = new PrintWriter(new FileWriter("/Users/zhangqingli/Desktop/log.txt")); for (int i=0; i<10; i++){ pw.println("Main : Status of Thread "+i+" : " +threads[i].getState()); pw.flush(); status[i]=threads[i].getState(); } for (int i = 0; i < 10; i++) { threads[i].start(); } boolean finish=false; while (!finish) { for (int i=0; i<10; i++){ if (threads[i].getState()!=status[i]) { writeThreadInfo(pw, threads[i], status[i]); status[i]=threads[i].getState(); } } finish=true; for (int i=0; i<10; i++){ finish=finish && (threads[i].getState()==State.TERMINATED); } } } private static void writeThreadInfo(PrintWriter pw, Thread thread, Thread.State state) { pw.printf("Main : Id %d - %s\n",thread.getId(),thread.getName()); pw.printf("Main : Priority: %d\n",thread.getPriority()); pw.printf("Main : Old State: %s\n",state); pw.printf("Main : New State: %s\n",thread.getState()); pw.printf("Main : ************************************\n"); pw.flush(); } }
class Calculator02 implements Runnable { private int number; public Calculator02(int number) { this.number=number; } @Override public void run() { for (int i=1; i<=10; i++){ try { Thread.sleep(3000); } catch (InterruptedException e) { e.printStackTrace(); } System.out.printf("%s: %d * %d = %d\n", Thread.currentThread().getName(), number, i, i*number); } } }
Thread 1: 1 * 1 = 1 Thread 0: 0 * 1 = 0 Thread 3: 3 * 1 = 3 Thread 2: 2 * 1 = 2 Thread 4: 4 * 1 = 4 Thread 6: 6 * 1 = 6 Thread 7: 7 * 1 = 7 Thread 9: 9 * 1 = 9 Thread 5: 5 * 1 = 5 Thread 8: 8 * 1 = 8 ... Thread 4: 4 * 9 = 36 Thread 9: 9 * 9 = 81 Thread 3: 3 * 9 = 27 ...
log.txt Main : Status of Thread 0 : NEW Main : Status of Thread 1 : NEW Main : Status of Thread 2 : NEW Main : Status of Thread 3 : NEW Main : Status of Thread 4 : NEW Main : Status of Thread 5 : NEW Main : Status of Thread 6 : NEW Main : Status of Thread 7 : NEW Main : Status of Thread 8 : NEW Main : Status of Thread 9 : NEW Main : Id 8 - Thread 0 Main : Priority: 10 Main : Old State: NEW Main : New State: TIMED_WAITING Main : ************************************ Main : Id 9 - Thread 1 Main : Priority: 1 Main : Old State: NEW Main : New State: TIMED_WAITING Main : ************************************ ... Main : ************************************ Main : Id 15 - Thread 7 Main : Priority: 1 Main : Old State: TIMED_WAITING Main : New State: RUNNABLE Main : ************************************ Main : Id 16 - Thread 8 Main : Priority: 10 Main : Old State: RUNNABLE Main : New State: TIMED_WAITING Main : ************************************ Main : Id 9 - Thread 1 Main : Priority: 1 Main : Old State: BLOCKED Main : New State: TIMED_WAITING Main : ************************************ ...
|