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

就是在很多没有规律的线程中,判断出哪些正在运行,哪些处于等待状态,哪些已经dead

qq_遁去的一_1
浏览 234回答 2
2回答

阿波罗的战车

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

慕盖茨4494581

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..." );}}你输出后会看到的
打开App,查看更多内容
随时随地看视频慕课网APP