问个菜问题,如何查看线程状态

发布于 2022-09-30 07:30:22 字数 44 浏览 9 评论 0

呵呵,这个问题我原来好像问过,也解决过
然后现在又忘了,兄弟们提醒一下的说

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

兔小萌 2022-10-07 07:30:22

How can i tell what state a thread is in?

Prior to Java 5, isAlive() was commonly used to test is a thread was still executing. If isAlive() returned false the thread was either new or terminated but there was simply no way to differentiate between the two.

Starting with the release of Tiger (Java 5) you can now get what state a thread is in by using the getState() method which returns an Enum of Thread.States. A thread can only be in one of the following states at a given point in time.

    * NEW
      A Fresh thread that has not yet started to execute.
    * RUNNABLE
      A thread that is executing in the Java virtual machine.
    * BLOCKED
      A thread that is blocked waiting for a monitor lock.
    * WAITING
      A thread that is wating to be notified by another thread.
    * TIMED_WAITING
      A thread that is wating to be notified by another thread for a specific amount of time.
    * TERMINATED
      A thread whos run method has ended.

The following code prints out all thread states.
Code:

public class ThreadStates{ public static void main(String[] args){ Thread t = new Thread(); Thread.State e = t.getState(); Thread.State[] ts = e.values(); for(int i = 0; i < ts.length; i++){ System.out.println(ts[i]); } } }

Below is a simple program, which prints some of the different states that two threads are in at certain points of a program. The code simply creates two threads. The first thread creates a file and fills it with the letters of the alphabet. The second thread opens the same file and reads the contents printing to the console. You will find along the way we have a placed a method a strategic points to determine exactly what state the thread is in at that point in time.
Code:

import java.io.*;

public class ThTest{
public static void main(String[] args){
  try{
  AlphaBlower ab = new AlphaBlower("AlphaBlower");
  ThreadState(ab);
  ab.start();
  ab.join();
  ThreadState(ab);
  
  AlphaSucker as = new AlphaSucker("AlphaSucker");
  ThreadState(as);
  as.start();
  as.join();
  ThreadState(as);
  }catch(InterruptedException ie){
   System.out.println(ie);         
  }
}
public static void ThreadState(Thread t){
   Enum e = t.getState();
   System.out.println(t.getName() + " is " + e.name());
}
}
class AlphaBlower extends Thread{
  
private BufferedWriter bw;
public AlphaBlower(String tname){
  super(tname);
}
public void run(){
  ThTest.ThreadState(Thread.currentThread());
  try{
   bw = new BufferedWriter(new FileWriter("C:" + File.separator + "AlphaHolder.txt"));
  }catch(IOException ioe){
    System.err.println(ioe);
  }
  try{
  for(int alpha = 97; alpha <= 122; ++alpha){
   try{
        bw.write(alpha);
   }catch(IOException ioe){
         System.err.println(ioe);   
   }
  }
  }finally{
   try{
   bw.close();
  }catch(IOException ioe){         
    System.out.println(ioe);
   }
  }
}
}

class AlphaSucker extends Thread{
private int c;
private BufferedReader br;
public AlphaSucker(String tname){
  super(tname);
}
public void run(){

  ThTest.ThreadState(Thread.currentThread());
  try{
   br = new BufferedReader(new FileReader("C:" + File.separator + "AlphaHolder.txt"));
  }catch(IOException ioe){
   System.err.println(ioe);
  }
  try{
  while((c = br.read()) != -1){
   System.out.print((char)c);
  }
  }catch(IOException ioe){
        System.err.println(ioe);
  }finally{
   try{
   br.close();
   }catch(IOException ioe){System.err.println(ioe);}
  }
}
}

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文