(未完)
一、测试
查看线程的运行状态
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 Handler system线程组:Finalizer system线程组:Signal Dispatcher system线程组:Attach Listener main线程组:main */
查看JVM中的线程名
原文地址:https://www.cnblogs.com/1138720556Gary/p/9149050.html
时间: 2024-10-08 00:30:32