1. What are the states of the thread pool?
The thread pool has 5 states: Running, ShutDown, Stop, Tidying, and Terminated.
Thread pool state switching frame diagram:
(Video tutorial recommendation: java video)
2. What is the difference between the submit() and execute() methods in the thread pool?
The parameters received are different
submit has a return value, but execute does not
submit facilitates Exception handling
3. How to ensure the safety of multi-threaded operations in java programs?
Thread safety is reflected in three aspects:
Atomicity: Provides mutually exclusive access, only one thread can operate on data at the same time, (atomic, synchronized);
Visibility: One thread's modifications to main memory can be seen by other threads in time, (synchronized, volatile);
Ordering: One thread observes the order of instruction execution in other threads ,Due to instruction reordering, this observation is generally ,disordered (happens-before principle).
(Related tutorial recommendations: java introductory program)
4. What is the principle of upgrading multi-thread locks?
In Java, there are four lock states. The levels from low to high are: stateless lock, biased lock, lightweight lock and heavyweight lock state. These states will change with time. As the competition gradually escalates. Locks can be upgraded but not downgraded.
Illustrated process of lock upgrade:
5. What is a deadlock?
Deadlock refers to a blocking phenomenon caused by two or more processes competing for resources or communicating with each other during execution. Without external force, they will all Unable to proceed. At this time, the system is said to be in a deadlock state or the system has a deadlock. These processes that are always waiting for each other are called deadlock processes.
Recommended tutorial: java interview questions
The above is the detailed content of 2020 New Java Interview Questions - Multi-Threading (3). For more information, please follow other related articles on the PHP Chinese website!