Talk about these common multi-threaded interview questions

image.png
The following questions are relatively elementary and are basic multi-threaded interview questions that must be mastered.

How many different ways are there to create a thread? Which one do you like? Why?
There are three ways to create a thread:

Inherit from the Thread class

Implement the Runnable interface

Applications can use the Executor framework to create thread pools

Implementing the Runnable interface is preferred because it does not require subclassing the Thread class. In the case where other objects have been inherited in the application design, this requires multiple inheritance (and Java does not support multiple inheritance), and only interfaces can be implemented. At the same time, the thread pool is also very efficient and easy to implement and use.

Briefly explain the several available states of a thread.
New (new): A thread object is newly created;

Runnable: After the thread object is created, other threads (such as the main thread) call the start () method of the object. The thread in this state is located in the runnable thread pool, waiting to be selected by thread scheduling to obtain the right to use the CPU;

Running (running): The thread in the runnable state (runnable) obtains the CPU time slice ( timeslice ) and executes the program code;

Blocking (block): The blocking state means that the thread gives up the right to use the CPU for some reason, that is, gives up the CPU timeslice and temporarily stops running. Until the thread enters the runnable state, there is no chance to obtain the cpu timeslice again and go to the running state.

There are three types of blocking:

Waiting for blocking: The running (running) thread executes the o.wait() method, and the JVM will put the thread in the waiting queue (waiting queue).

Synchronization blocking: When a running thread acquires the synchronization lock of an object, if the synchronization lock is occupied by another thread, the JVM will put the thread into the lock pool (lock pool).

Other blocking: When a running (running) thread executes the Thread.sleep (long ms) or t.join () method, or an I/O request is issued, the JVM will place the thread in a blocking state. When the sleep() state times out, join() waits for the thread to terminate or times out, or when the I/O processing is complete, the thread re-enters the runnable state. Here I recommend an architecture learning exchange circle to everyone. Communication study guide pseudo-xin: 1253431195 (there are a lot of interview questions and answers), which will share some video recordings recorded by senior architects: there are Spring, MyBatis, Netty source code analysis, high concurrency, high performance, distributed, micro-service architecture The principle of JVM performance optimization, distributed architecture, etc. have become the necessary knowledge system for architects. You can also receive free learning resources, which are currently benefiting a lot

Dead (dead): The thread run (), main () method execution ends, or the run () method exits due to an exception, the thread ends its life cycle. Dead threads cannot be revived.

What is the difference between a synchronized method and a synchronized block of code?
the difference:

Synchronized methods use this or the current class object as the lock by default;

The synchronization code block can choose what to lock, which is more fine-grained than the synchronization method. We can choose to synchronize only part of the code that will cause synchronization problems instead of the entire method;

Inside the monitor (Monitor), how to do thread synchronization? What level of synchronization should the program do?
Monitors and locks are used together in the Java Virtual Machine. A monitor monitors a block of synchronized code to ensure that only one thread executes the synchronized block at a time. Each monitor is associated with an object reference. A thread is not allowed to execute synchronized code until the lock is acquired.

Java also provides explicit monitor ( Lock ) and implicit monitor ( synchronized ) two locking schemes.

What is a deadlock (deadlock)?
A deadlock occurs when two or more threads are waiting for each other to finish executing before continuing to execute. The result is that these threads are stuck in infinite waits.

How to ensure that N threads can access N resources without causing deadlock?
Four necessary conditions for multithreading to produce deadlock:

Mutual exclusion condition: A resource can only be used by one process at a time.

Hold and Request Condition: When a process is blocked by requesting a resource, it will hold on to the acquired resource.

Inalienability: The process has acquired a resource and cannot be deprived of it until it is finished.

Circular waiting condition (closed loop): A head-to-tail cyclic waiting resource relationship is formed between several processes.

By breaking either of the conditions, the deadlock can be avoided

A very simple way to avoid deadlocks is to specify the order in which locks are acquired, and force threads to acquire locks in the specified order. Therefore, if all threads acquire and release locks in the same order, there will be no deadlock.

Guess you like

Origin blog.csdn.net/m0_54828003/article/details/127325349