线程6种状态
NEW——新建状态
RUNNABLE——运行(可运行)状态
BLOCKED——阻塞状态
TIMED_WAITING——休眠状态
WAITING——等待状态
TERMINATED——终止状态
查看线程的运行状态
ThreadState类
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() {
try {
//在新线程中运行waitForASecond()方法
waitForASecond();
//在新线程中运行waitForYears()方法
waitForYears();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
测试类
public class Test01 {
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()方法,启动新线程
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());
}
}
查看JVM中的线程名(ThreadGroup)
ThreadGroup类的常用方法
activeCount()——返回此线程组中活动线程的估计数
activeGroupCount()——返回此线程组中活动线程组的估计数
enumerate(Thread[] list,boolean recurse)——把此线程组中所有活动线程复制到指定数组中
enumerate(ThreadGroup[] list,boolean recurse)——把对此线程中的所有活动子组的引用复制到指定数组中
getName()——返回此线程组的名称
getParent()——返回此线程组的父线程组
/**
* 获得根线程组
* @return
*/
private static ThreadGroup getRootThreadGroups(){
//获得当前线程组
ThreadGroup rootGroup =Thread.currentThread().getThreadGroup();
while(true){
//如果getParent()方法的返回值非空则不是根线程组
if(rootGroup.getParent()!=null){
//获得父线程组
rootGroup=rootGroup.getParent();
}else{
//如果到达根线程组则退出循环
break;
}
}
//返回根线程组
return rootGroup;
}
/**
* 获得给定线程组中所有线程名
* @param group
* @return
*/
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;
}
/**
* 获得线程组中子线程组
* @param group
* @return
*/
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) throws InterruptedException {
for(String string:getThreadGroups(getRootThreadGroups())){
//遍历输出列表中的字符串
System.out.println(string);
}
}
线程常用方法
start()——新建、启动新线程
sleep(5)——休眠0.5秒
wait(5)——等待0.5秒
wait()——永久等待
notify()——唤醒由wait()进入等待状态的线程
notifyAll()——唤醒等待的所有线程
join()——等待调用该方法的线程终止
join(5)——等待调用该方法的线程终止的时间最长为0.5秒
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/117550.html