Java basic question and answer

57. The scope of the various locking scenarios of synchronized

1. For non-static methods, the object instance (this) is locked, and each object instance has a lock.

public synchronized void method() {}


2. Acting on the static method, what is locked is the Class object of the class, because the related data of the Class is stored in the permanent generation metaspace, which is shared globally, so the static method lock is equivalent to a global lock of the class, which will lock all The thread that called this method.

public static synchronized void method() {}


3. Acting on Lock.class, what is locked is the Class object of Lock, and there is only one globally.

synchronized (Lock.class) {}


4. Acting on this, the object instance is locked, and each object instance has a lock.

synchronized (this) {}


5. Acting on static member variables, what is locked is the static member variable object. Since it is a static variable, there is only one globally.

public static Object monitor = new Object(); synchronized (monitor) {}

         

59. How to prevent deadlock?
The way to prevent deadlock is to break any one of the four necessary conditions.

1) Break the mutual exclusion condition: cancel the mutual exclusion in the system. If the resource is not exclusively used by one process, then deadlock will definitely not happen. But in general, among the four conditions listed, the "mutually exclusive" condition cannot be broken. Therefore, in deadlock prevention, it is mainly to destroy several other necessary conditions, not to involve the destruction of "mutual exclusion" conditions. .

2) Break the request and hold conditions: 1) Adopt the resource pre-allocation strategy , that is, apply for all resources before the process runs, and run if it is satisfied, or wait. 2) Before each process makes a new resource application, it must first release the resources it previously occupied.

3) Break the inalienable condition: When a process occupies certain resources and further applies for other resources but cannot be satisfied, the process must release the resources it originally occupied.

4) Break the loop waiting condition: implement an orderly resource allocation strategy, and uniformly number all resources in the system , and all processes can only apply for resources in the form of increasing serial numbers.        

60. Why use a thread pool? Isn't it comfortable to directly create a new thread?


If we directly create a new thread in the method to handle it, when this method is called frequently, many threads will be created, which will not only consume system resources, but also reduce the stability of the system. If you accidentally crash the system, you can Go directly to finance to check out.

If we use the thread pool reasonably, we can avoid the dilemma of crashing the system. In general, using the thread pool can bring the following benefits:

  1. Reduce resource consumption . Reduce the cost of thread creation and destruction by reusing created threads.
  2. Improve responsiveness . When a task arrives, the task can be executed immediately without waiting for the thread to be created.
  3. Increase the manageability of threads . Threads are scarce resources, and thread pools can be used for unified allocation, tuning, and monitoring.

61. What are the core attributes of the thread pool?

threadFactory (thread factory): The factory used to create worker threads. ? ? ?

corePoolSize (number of core threads): When the thread pool has fewer threads running than corePoolSize, a new thread will be created to handle the request, even if other worker threads are idle.

workQueue (queue) : A blocking queue used to hold tasks and hand them off to worker threads .

maximumPoolSize (maximum number of threads): The maximum number of threads allowed to be opened by the thread pool.

handler (rejection strategy) : When adding tasks to the thread pool, the rejection strategy will be triggered in the following two situations: 1 ) The running status of the thread pool is not RUNNING ; 2) When the thread pool has reached the maximum number of threads and the blocking queue is full.

keepAliveTime (keep alive time ): If the current number of threads in the thread pool exceeds corePoolSize, the redundant threads will be terminated when the idle time exceeds keepAliveTime.

62. Talk about the operation process of the thread pool.


 

63. What rejection strategies does the thread pool have?


AbortPolicy: Abort policy. The default rejection strategy directly throws RejectedExecutionException. The caller can catch this exception and write his own processing code according to his needs.

DiscardPolicy: Discard policy. Do nothing and simply discard the rejected task .

DiscardOldestPolicy: Discard the oldest policy . Abandoning the oldest task in the blocking queue is equivalent to the next task in the queue to be executed, and then resubmitting the rejected task. If the blocking queue is a priority queue, the "discard oldest" strategy will result in discarding the highest priority tasks, so it is best not to use this strategy with priority queues.

CallerRunsPolicy : Caller Runs Policy. Execute the task in the caller thread. This strategy implements an adjustment mechanism. This strategy neither abandons the task nor throws an exception , but rolls back the task to the caller (the main thread that calls the thread pool to execute the task). Since it takes a certain amount of time to execute the task , so the main thread cannot submit tasks for at least a period of time, so that the thread pool has time to process the tasks being executed.
 

70. What is the difference between List, Set, and Map?


List (a good helper to deal with order): The List interface stores a set of non-unique (multiple elements can refer to the same object), ordered objects.

Set (focus on unique properties): Duplicate collections are not allowed, and multiple elements will not refer to the same object.

Map (professional users who use Key to search): Use key-value pairs to store. Map will maintain the value associated with Key. Two Keys can refer to the same object, but the Key cannot be repeated. A typical Key is of String type, but it can also be any object.

/*
1. There is single inheritance between classes. There is only one direct parent class.
2. There are multiple implementations between classes and interfaces. A class can implement multiple interfaces.
3. There is multiple inheritance between interfaces.
 
Notes:
1. It doesn't matter if the abstract methods in multiple parent interfaces are repeated .
2. If the default method in multiple parent interfaces is repeated, then the sub-interface must override the default method , [and with the default keyword].    

    
 

Guess you like

Origin blog.csdn.net/Hoshea_sun/article/details/129782600