Multi-threaded Java Interview Questions

1. What is the purpose multithreading
full use of CPU resources, concurrent to do more things
2. How to create a multi-threaded
new new the Thread ()
3. Thread What is
a code execution flow, to complete the implementation of a code of
this group the code, often referred to as a task
fit on a single machine 4-core CPU is not suitable for multi-threaded
fit, when a single thread, the thread needs to wait for IO If so, then the CPU is idle out
when to make the CPU 5. thread
when blocked, wait, await, wait for IO
at Sleep
yield
end of the thread
6. the thread is not better
new thread to time, a one-time run out, run out to destroy, but also time
thread is an object in Java, each Java thread requires an operating system thread support. Creating destruction takes time.
Built many threads, you need space, will be a memory tense
Java objects occupy heap memory, operating system threads consume system memory, according to jvm specification, a thread default maximum stack size 1M (if more than 1M, will report OOM exception), the stack space required dispensing from the system memory.
7. How to use multi-threaded correctly
with the appropriate number of threads constitutes the thread pool

Thread Pool

Works
accept the task, into the warehouse
worker thread is removed from the warehouse to perform
when there is no task, thread blocked, wake up when there are tasks to perform
tasks what
Runnable
a Callable
warehouse What
BlockingQueue blocking queue thread-safe
access to as space-time obstruction, obstruction placed in the queue is full
Here Insert Picture Description
8. how to determine the appropriate number of threads
if it is computational tasks: one to two times the CPU
if the IO-type tasks: need more threads, tomcat default maximum number of threads to 200

Here Insert Picture Description
Here Insert Picture Description
Here Insert Picture Description
This video talked about 98 minutes when the thread pool to achieve the standard

Published 34 original articles · won praise 6 · views 4767

Guess you like

Origin blog.csdn.net/qq_42712280/article/details/103168726