這篇還是要繼續來介紹jconsole
,Thread的頁籤介紹
package Controller;
import java.util.Scanner;
public class JconsoleTest implements Runnable{
private String message;
public JconsoleTest(String message) {
this.message = message;
}
public static void main(String[] args) throws InterruptedException{
Thread.sleep(15000);
JconsoleTest thread2 = new JconsoleTest("thread2");
JconsoleTest thread3 = new JconsoleTest("thread3");
Thread th3 = new Thread(thread2);
Thread thr = new Thread(thread3);
thr.setName("Thread2(線程2)");
thr.start();
th3.setName("Thread3(線程3)");
th3.start();
Thread.sleep(5000);
thr.interrupt();
for(int x=0;x<1000;x++){
System.out.println("Thread1:"+x);
Thread.sleep(100);
}
Scanner scannerTest = new Scanner(System.in);
scannerTest.next();
}
public void run(){
try{
for(int x=0;x<1000;x++){
System.out.println(this.message+":"+x);
Thread.sleep(100);
}
}catch(InterruptedException e){
}
}
}
以上程式碼,我另外開了兩個線程,分別指定線程名稱為:Thread2(線程2)
、Thread3(線程3)
,在啟動了兩個線程後,我讓執行緒暫停5秒(Thread.sleep(5000);
),接著手動將Thread3(線程3)
中斷(interrupt
),主線程
與Thread2(線程2)
都分別跑了1000次,最後放個Scanner
,讓程序等待輸入(為了看Thread得資訊)
編譯與執行的指令說明,請參考Day4-java語言編譯器:javac & 運行工具:java (上)
如何操作,請參考Day21-JDK可視化監控工具:jconsole(一)