문제

I got a NullPointerException in my project and I would like to evaluate what is the sevirity of this issue. The exception is thrown just before the thread finished its task and going to die anyway. My code failed to catch this excepition, so the thread is dead.

Here is a simple simulation of the situation:

public class Test extends Thread {
    public static void main(String[] args) throws InterruptedException {

        Test thread = new Test();
        thread.start();
        while(true) {
           System.out.println("I'm still here!");
           Thread.sleep(1000);
        }
    }

    public void run() {
        String s = null;
        int len = s.length(); // causes NullPinterException
    }
}

My question is: What is going to happened to this poor thread now? Is its linux file descriptor get freed? Is there any stability or memroy issues that may occur in such kind of code?

도움이 되었습니까?

해결책

The handling is not different than with any other terminated thread. The one thing that happens before is the search for an UncaughtExceptionHandler according to the rules (specific Thread, ThreadGroup, all threads) but apart from this the "normal" cleanup procedure follows. There are no specific consequences regarding sytem resources (depending on the Thread implementation) or memory issues when a thread is terminated by an uncaught exception in contrast to a "normal" termination.

다른 팁

This is not about threads at all. Look at your code:

   String s = null;
   int len = s.length();

When you are calling s.length() the s is indeed null that causes NullPointerException. Assign some value to s and you will get its length.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top