为了账号安全,请及时绑定邮箱和手机立即绑定

请教下在java多线程程序中,怎样实时找出处于等待(阻塞)状态线程、进程的个数?

请教下在java多线程程序中,怎样实时找出处于等待(阻塞)状态线程、进程的个数?

qq_遁去的一_1 2022-05-19 12:15:30
就是在很多没有规律的线程中,判断出哪些正在运行,哪些处于等待状态,哪些已经dead
查看完整描述

2 回答

?
阿波罗的战车

TA贡献1862条经验 获得超6个赞

线程的最大好处就是可以共用同一个内存块。
你只要定义一个静态的变量,给所有线程读写操作。你就能统计这些了。

查看完整回答
反对 回复 2022-05-23
?
慕盖茨4494581

TA贡献1850条经验 获得超11个赞

class Example1 extends Thread {
boolean stop=false;
public static void main( String args[] ) throws Exception {
Example1 thread = new Example1();
System.out.println( "Starting thread..." );
thread.start();
Thread.sleep( 3000 );
System.out.println( "Interrupting thread..." );
thread.interrupt();
Thread.sleep( 3000 );
System.out.println("Stopping application..." );
//System.exit(0);
}
public void run() {
while(!stop){
System.out.println( "Thread is running..." );
long time = System.currentTimeMillis();
while((System.currentTimeMillis()-time < 1000)) {
}
}
System.out.println("Thread exiting under request..." );
}
}

你输出后会看到的



查看完整回答
反对 回复 2022-05-23
  • 2 回答
  • 0 关注
  • 210 浏览

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号