Some threads it lives, but it lies idle in the pool; Some threads it died, and it became an interview question.

This article, from an interview with Alibaba.

I remember it was a Monday, fresh from a weekend of relaxation, and I was frantically typing. At this time, my mobile phone rang up, picked up a look, it is from Hangzhou, thinking this is to give me a stock recommendation or let me loan it. I picked up the phone, ready to flirt. A voice rang out :” Hello, is that XXX? This is Hangzhou Alibaba. Do you have time for a phone interview now?” . To be honest, after listening to this sentence, I feel THAT I have been in Hangzhou, full of energy in Hangzhou ali’s station “repair blessing”. But I’m in the middle of crazy output right now and I don’t have time, so I said, “Sorry, I don’t have time right now. Can we make it 8 o ‘clock tonight?” .

I got the phone call in the evening as promised. We went straight to the point and had a friendly technical exchange. The specific interview process is not detailed, there is an opportunity to sort out an interview to share. During the interview, one question impressed me deeply:

If a thread in a thread pool is abnormal, what does the thread pool do with that thread?

Note that the thread pools discussed in this article are Executors thread pools.

For Executors thread pool I can say is thoroughly familiar with the heart, because the work used more, read the source code. It’s one of the few things I always ask as an interviewer, such as:

The JDK Executors Thread pool? Do you know what default implementations the JDK provides? Have you read alibaba Java development manual? Do you know why the default implementation is not allowed? You didn’t use the default, did you? Let’s introduce some common parameters of your custom thread pool. Where did you get the values of these parameters? Figured it out? How do you figure that out? Are the tasks in the thread pool IO intensive or compute intensive? Ok, now that we have a custom thread pool, how about the workflow for your thread pool? What if your thread pool is full? To refuse? Zha refused? What are the rejection strategies? Take it easy. Just name two. . The default implementation is not allowed in the Java development manual of Alibaba. You replied that it might cause OOM, so let’s talk about JVM……


This series of rapid-fire thread pools is just a few of the questions I have to ask as an interviewer. Don’t ask why, because our recruiting JD clearly says: Familiar with multithreaded programming. And these questions, I think, are fundamental to being familiar with multithreaded programming. Here I also do not answer, this kind of article on the net is still very much, can understand.

This is really important, and I have stressed it to my friends many times:

Come on, let’s break it down

Okay, now back to the interview question that ali’s interviewer asked me:

If a thread in a thread pool is abnormal, what does the thread pool do with that thread?

Talk about my answer at that time first, because in the mind have no bottom, my answer is very hesitant also very bad! As follows:

My answer can be summed up in three sentences:

1. Throw a stack exception — that’s half true! 2. Does not affect other thread tasks — this is all true! 3. This thread will be put back into the thread pool.

Write test cases


Why is throwing a stack exception half right?

Let the program run first, and let the facts speak for themselves:


We can see from the execution results

When executed as execute, you can see the output of stack exceptions.

When the execution is Submit, the stack exception does not output.

Submit stack exception; submit stack exception;


So, now you know why the answer: Throwing a stack exception is only half right.

When the execute method executes, a stack exception is thrown (printed).

When the submit method executes, the return result is wrapped in the Future, and if the future.get() method is called, exception catching must be done so that stack exceptions can be thrown (printed).

You think this part is finished here? That’s no good. There’s no question in your mind? Why does execute throw an exception directly, while Submit does not? No secrets under source code:

When executed is executes:

In Java. Util. Concurrent. ThreadPoolExecutor# runWorker throws the exception:


The java.lang.ThreadGroup#uncaughtException exception is handled:


UncaughtException = uncaughtException = uncaughtException


This method is invoked by the JVM, and we just need to specify how we want to handle it.

So how do we specify:


When the execution method is submit:


Its essence is also called the execute method, so it is back to Java. Util. Concurrent. ThreadPoolExecutor# runWorker method:


Go ahead and keep following:


Java. Util. Concurrent. FutureTask# setException stem what ah, see at a glance:


Take a deep breath, clear your head, and let’s move on to the final truth:


Ok, the first item ** why is a stack exception thrown half right? ** end of discussion. After a trip through the source code, we can now give full marks for this part of the answer.

Does not affect other thread tasks, correct answer

This part we directly on the code, run to see the result:


Code and results don’t cheat:

The exception of one thread in the thread pool does not affect other thread tasks

Notice the detail of the thread name :1,2,3,4,6. The devil is in the details ah, this point I will talk about next, first throw out the question here: I wonder, why there is no 5 ah? !

This thread will be put back into the thread pool why is it all wrong?

Let’s go to the source code to find the answer:


Let the source answer:


Where did thread 5 go?

The new Worker() method tells you where 5 went.


It will taste even better with this picture drawn by myself as a soul painter:


Now we know why: I got it all wrong when I said the thread would be put back into the thread pool. There is also a thread name change detail for you, you are welcome, just keep an eye on it.

To summarize

When a thread in a thread pool fails:

1. When the execute mode is Execute, you can see the output of stack exception.

2. When the execution method is Submit, the stack exception does not output. However, an exception can be caught when the future.get () method is called.

3. It does not affect the normal execution of other threads in the thread pool.

4. The thread pool removes the thread and creates a new thread into the thread pool.

5. Do not memorize the answer, to understand, to go deep, remember to ask the interviewer after the above, do you need me to speak from the source point of view? Oh, that’s fucking polite and classy.

Above, I asked “A thread in a thread pool is abnormal, so what does the thread pool do with the thread?” My opinion on this issue is finished. It only represents my personal opinion. Welcome friends with different opinions to discuss and make progress together.

One last word

I’ve been working on this post since I tweeted the last one last Friday. Most of it is thought about in the half hour before you go to bed at night, and in the hour before you get up in the morning on weekends and weekdays.

It’s not that hard to think of what to write, it’s how you control it. I am about technical language, weigh, access to a large number of articles to disprove, in brief silence silence silence again, after all do technology, I think that is a very serious thing, I often imagine that I am craftsmen of the cultural relics in the imperial palace, and on the cognition of craftsmen spirit, at the moment, I can and they are a bit far, but I always request oneself in the spirit of craftsman. As I expressed in the group, I try my best to ensure weekly promotion and quality of technical articles.

Finally, one more time:

Some threads it lives, but it lies idle in the pool; Some threads are still alive, but they are always busy until they fly; Some threads it died, was abandoned, was recycled, but it has no regrets, because it died on the way to perform the task, it with its last cry “for the new brother, remove me!” Finally, it becomes an interview question. I haven’t answered yet.

Welcome to pay attention to the public account [WHY Technology], adhere to the output of original. Share technology, taste life, wish you and I progress together.