This is the 17th day of my participation in Gwen Challenge
Today we continue to look at some Java interview questions, basically the interview is often asked, there is a certain probability will encounter, I hope you can interview smoothly.
1. Name several thread pool parameters.
CorePoolSize: core thread size of the thread pool
MaximumPoolSize: specifies the maximum number of threads in a thread pool
KeepAliveTime: indicates the keepAliveTime of idle threads
Unit: unit of idle thread lifetime
WorkQueue: indicates a workQueue
ThreadFactory: a threadFactory
Handler: rejects the policy
2. Name common thread pools.
NewCachedThreadPool:
Create a thread pool that can create new threads as needed, but reuse previously constructed threads as they become available.
For programs that perform many short-term asynchronous tasks, these thread pools can often improve program performance. Calling execute reuses previously constructed threads (if available).
If no existing thread is available, a new thread is created and added to the pool.
Terminates and removes threads from the cache that have not been used for 60 seconds. Therefore, a thread pool that remains idle for a long time does not use any resources.
NewFixedThreadPool:
Create a reusable thread pool with a fixed number of threads to run in a shared unbounded queue.
At any given point, most nThreads threads will be active for processing tasks.
If the attached task is submitted while all threads are active, the attached task will wait in the queue until there are available threads. If any thread terminates due to failure during execution prior to closure, a new thread will replace it to perform subsequent tasks (if needed).
Threads in the pool will remain in existence until a thread is explicitly closed.
NewScheduledThreadPool:
Create a thread pool that can schedule commands to run after a given delay or to execute them periodically.
NewSingleThreadExecutor:
Executors. NewSingleThreadExecutor () returns a thread pool (only one thread to the thread pool), the thread pool can be after the death of the thread (or anomalies) to start a thread to replace the original thread continue execution!
3. Describe the life cycle of a thread.
NEW State (NEW)
RUNNABLE state
RUNNING Status
The state of being BLOCKED
Thread DEAD
4. Name several ways to terminate a thread.
The program runs normally.
Exit the thread with the exit flag:
The thread normally terminates after the run() method is executed, however, often some threads are server threads. They take a long time to run and can only be shut down if some external condition is met. Use a variable to control the loop, for example: The most straightforward way is to set a Boolean flag and control whether the while loop exits by setting the flag to true or false.
The Interrupt method terminates a thread.
The stop method terminates a thread (thread unsafe).
Note: the answer is only a basic answer, not what the standard answer, everyone is not the same when the interview, so the answer to the interview questions also need to have their own heart draft, just a record, I hope you can use for reference.