標籤:ack ext inter method 測試 計時 ++ tar 建立
(未完)
一、測試
查看線程的運行狀態
package Cynical_Gary;public class ThreadState implements Runnable{ public synchronized void waitForASecond() throws InterruptedException{ //使當前線程等待0.5秒或其它線程調用notify()或notifyAll()方法 wait(500); } public synchronized void waitForYears() throws InterruptedException{ //使當前現場永久等待,直到其他線程調用notify()或notifyAll()方法 wait(); } public synchronized void notifyNow() throws InterruptedException{ //喚醒由調用wait()方法進入等待狀態的線程 notify(); } @Override public void run() { // TODO Auto-generated method stub try{ //在新線程中運行waitForASecond()方法 waitForASecond(); //在新線程中運行waitForYears();方法 waitForYears(); }catch(InterruptedException e){ e.printStackTrace(); } }}ThreadState類
package Cynical_Gary;public class Gary_Text { public static void main(String[] args) throws InterruptedException{ //建立State對象 ThreadState state = new ThreadState(); //利用State對象建立Thread對象 Thread thread = new Thread(state); //輸出線程狀態 System.out.println("建立線程:"+thread.getState()); //啟動新線程 thread.start(); //輸出線程狀態 System.out.println("啟動線程:"+thread.getState()); //當前線程休眠0.1秒,使新線程運行waitForASecond()方法 Thread.sleep(100); //輸出線程狀態 System.out.println("計時等待:"+thread.getState()); //當前線程休眠1秒,使新線程運行waitForYears()方法 Thread.sleep(1000); //輸出線程狀態 System.out.println("等待線程:"+thread.getState()); state.notifyNow(); System.out.println("喚醒線程:"+thread.getState()); //當前線程休眠1秒,使新線程結束 Thread.sleep(1000); //輸出線程狀態 System.out.println("終止線程:"+thread.getState()); }}/* 輸出: 建立線程:NEW啟動線程:RUNNABLE計時等待:TIMED_WAITING等待線程:WAITING喚醒線程:RUNNABLE終止線程:TERMINATED */Text類()測試
package Cynical_Gary;import java.util.ArrayList;import java.util.List;public class Gary_Text { //獲得根線程組 private static ThreadGroup getRootThreadGroup(){ //獲得當前線程組 ThreadGroup rootGroup = Thread.currentThread().getThreadGroup(); while(true){ //getParent()方法的傳回值非空則不是根線程組 if(rootGroup.getParent()!=null){ //獲得父線程組 rootGroup = rootGroup.getParent(); }else{ //如果達到根線程組則退出迴圈 break; } } //返回根線程組 return rootGroup; } //獲得給定線程組中所有線程名 public static List<String> getThreads(ThreadGroup group){ //建立儲存線程名的列表 List<String> threadList = new ArrayList<String>(); //根據活動線程數穿件線程數組 Thread[] threads = new Thread[group.activeCount()]; //複製線程到線程數組 int count = group.enumerate(threads,false); //遍曆線程數組將線程名及所在組儲存到列表中 for(int i=0;i<count;i++){ threadList.add(group.getName()+"線程組:"+threads[i].getName()); } //返回列表 return threadList; } //獲得給定線程組中子線程 public static List<String> getThreadGroups(ThreadGroup group){ //建立儲存線程名的列表 List<String> threadList = getThreads(group); //建立線程組數組 ThreadGroup[] groups = new ThreadGroup[group.activeGroupCount()]; //複製子線程數組到線程組資料 int count = group.enumerate(groups,false); //遍曆所有子線程數組 for(int i=0;i<count;i++){ //利用getThreads()方法獲得線程名列表 threadList.addAll(getThreads(groups[i])); } //返回所有線程名 return threadList; } public static void main(String[] args){ for(String string:getThreadGroups(getRootThreadGroup())){ //遍曆輸出資料行表中的字串 System.out.println(string); } }}/* 輸出:system線程組:Reference Handlersystem線程組:Finalizersystem線程組:Signal Dispatchersystem線程組:Attach Listenermain線程組:main*/查看JVM中的線程名
Java基礎__Java中多線程那些事