Java interview frequently asked questions and answers summary

1. What is a thread pool?

Existing issues:
If there are a large number of concurrent threads, and each thread executes a short task and then ends, frequent thread creation will greatly reduce the cost. The efficiency of the system because frequent thread creation and destruction takes time.

So is there a way to reuse threads, that is, after executing a task, it will not be destroyed, but can continue to execute other tasks?

Idea: Create multiple threads in advance, put them into the thread pool, obtain them directly when used, and put them back into the pool after use. It can avoid frequent creation and destruction and realize reuse. Similar to public transportation in life.
Insert image description here
Benefits:

Improved responsiveness (reduced time to create new threads)

Reduce resource consumption (reuse threads in the thread pool, no need to create them every time)

Facilitates thread management


2. Internal implementation principle of thread pool

The working principle of Java thread pool
Comprehensive explanation of the working principle of Java thread pool
The implementation principle of Java thread pool

Why does Java's thread pool use blocking queues?

Blocking queue is mainly used in the case of producer-consumer model. For example, if a thread takes elements from an empty blocking queue, the thread will be blocked until there are elements in the blocking queue. When there are elements in the queue, the blocked thread will be awakened automatically (no need for us to write code to wake up). This provides great convenience. If a non-blocking queue is used, it will not block the current thread, and additional synchronization strategies and inter-thread wake-up strategies must be implemented, which is very troublesome to implement.


3. What is the singleton design pattern?

Design patterns are optimized code structures, programming styles, and ways of thinking about solving problems that have been summarized and theorized in a large amount of practice. Design patterns save us from having to think and explore ourselves. Just like classic chess records, we use different chess records for different chess games. "Routines"

There are 23 classic design patterns. Each design pattern is a way to deal with a specific problem in a specific environment.
Insert image description here
The simple factory pattern is not one of the 23 classic patterns, but a simplified version of the factory method pattern.

The so-called singleton design pattern of a class is to adopt certain methods to ensure that only one object instance of a certain class can exist in the entire software system, and the class only provides one method to obtain its object instance.

Implementation ideas

If we want the class to produce only one object in a virtual machine, we must first change the access permissions of the constructor of the class Set to private, so that cannot use the new operator to generate objects of the class outside the class, but objects of the class can still be generated inside the class. . Because the object of the class cannot be obtained from outside the class, you can only call a static method of the class to return the object created inside the class. The static method can only access the static member variables in the class. Therefore, pointing to the object generated inside the class Variables of this class object must also be defined as static.

Two implementation methods

Hungry Chinese style

class Singleton {
    
    
	 // 1.私有化构造器
	 private Singleton() {
    
    
	 }
	 // 2.内部提供一个当前类的实例
	 // 4.此实例也必须静态化
	 private static Singleton single = new Singleton();
	 
	 // 3.提供公共的静态的方法,返回当前类的对象
	 public static Singleton getInstance() {
    
    
	 	return single;
	 }
}

lazy man style

class Singleton {
    
    
	 // 1.私有化构造器
	 private Singleton() {
    
    
	 }
	 // 2.内部提供一个当前类的实例
	 // 4.此实例也必须静态化
	 private static Singleton single;
	 // 3.提供公共的静态的方法,返回当前类的对象
	 public static Singleton getInstance() {
    
    
	 	if(single == null) {
    
    
	 		single = new Singleton();
	 	}
	 	return single;
	 }
}

Hungry Man Style vs Lazy Man Style

Insert image description here

Advantages and application scenarios of singleton mode

Since the singleton mode only generates one instance, it reduces system performance overhead.When the generation of an object requires more resources, such as reading configuration and generating other dependent objects When , it can be solved by directly generating a singleton object when the application starts, and then permanently resident in the memory.

Application scenarios

  • Windows' Task Manager is a very typical singleton mode
  • Windows' Recycle Bin (recycle bin) is also a typical singleton application. During the entire system operation, the Recycle Bin maintains only one instance.
  • Application is also a typical application of singleton
  • Log applications of applications are generally implemented using singleton mode. This is generally because the shared log file is always open, because only one instance can operate it, otherwise it is difficult to append the content.
  • The design of database connection pool generally adopts singleton mode, because database connection is a database resource.

4. What are the states of threads?

Several states of threads

Guess you like

Origin blog.csdn.net/weixin_44123362/article/details/130257264