This article is participating in “Java Theme Month – Java Debug Notes Event”, see < Event link > for more details.

Here are some common interview questions:

1. Have you used threads? How are threads implemented?

Implement functionality or business logic in run methods by inheriting Thread classes and implementing the Runnable interface.

What are the differences and connections between the start and run methods in threads?

The start method is called to start a thread that is in a ready (runnable) state, but not running, and executes the run() method, which is what the thread wants to execute, as soon as it gets the CPU time slice.

The run method is just a call to a normal method in the thread and is executed in the main thread. If you call the run method directly, there is still only one thread in the program, the main thread, the program execution path is still only one, or to execute sequentially, or to wait for the completion of the run method body can continue to execute the following code, so that the purpose of writing thread is not achieved.

public static void main(String args[]) {
 
        Thread t = new Thread() {
 
            public void run(a) { pong(); }}; t.start(); System.out.print("ping");
 
 }
 
 
public static void pong(a) {
 
        System.out.print("pong");
 
 }
Copy the code

Output: pingpong

public static void main(String args[]) {
 
        Thread t = new Thread() {
 
            public void run(a) { pong(); }}; t.run(); System.out.print("ping");
 
    }
 
 
 
public  static void pong(a) {
 
        System.out.print("pong");
 
 }
Copy the code

Output: pongping

It is easy to tell the difference between the start() method and the run() method using these two examples:

  • t.start(); This line of code is equivalent to starting the thread
  • t.run(); This line of code is equivalent to using the run method of the t class.

3. Thread deadlocks? How to effectively avoid thread deadlocks?

A deadlock is a phenomenon in which two or more processes are blocked during execution by competing for resources or by communicating with each other and cannot proceed without external intervention. The system is said to be in a deadlock state or a deadlock occurs in the system. These processes that are always waiting for each other are called deadlocked processes.

The simplest way to avoid deadlocks is to prevent cyclic waiting conditions, set flags and sorts for all resources in the system, and specify that all processes must apply for resources in a certain order (ascending or descending) to avoid deadlocks.

Are thread pools used in the project? How does it work?

Used. When we use a thread to create a single thread, so very easy to implement, but there will be a problem: if the concurrent number of threads a lot, and every thread is executing a short mission was over, so frequently create a thread will reduce the efficiency of the system, because of the frequent needs time to create a thread and destroying threads.

One way to solve this problem is to use thread pools to make threads reusable, that is, to finish a task and not be destroyed, but to continue to perform other tasks. ThreadPoolExecutor is the core of a Java thread pool, and ThreadPoolExecutor is the base of the thread pool:

public ThreadPoolExecutor(int  corePoolSize,                           
                          int  maximumPoolSize,                          
                          long  keepAliveTime, 
                          TimeUnit  unit,                         
                          BlockingQueue<Runnable>  workQueue,                           
                          ThreadFactory  threadFactory,                           
                          RejectedExecutionHandler  handler)
Copy the code

The meanings of parameters are as follows:

  • corePoolSize: The minimum number of threads in the thread pool
  • maximumPoolSize: Specifies the maximum number of threads in the thread poolRejectedExecutionHandler
  • keepAliveTime: The maximum lifetime of a thread, beyond which it will be reclaimed
  • unit: Unit of maximum thread lifetime
  • workQueue: Caches queues for asynchronous tasks that need to be executed
  • threadFactory: Creates a thread factory
  • handler: Indicates the reject policyworkQueueIs full and the number of threads in the pool is reachedmaximumPoolSizeIs rejected by the thread pool when adding a new task.DiscardPolicy: Discard the current task,DiscardOldestPolicy: Throw away the oldest,CallerRunsPolicy: executed by the thread that submitted the task to the thread pool,AbortPolicy: throwRejectedExecutionExceptionThe exception.