【多线程】知识点集合

线程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秒

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值