Home >Java >javaTutorial >Java interview questions (must read when looking for a job)
Personal reference summary, any similarity is purely coincidental!
1. How is the implementation principle of hashmap and the thread safety of hashtable implemented?
HashMap is actually implemented as a linear array, so it can be understood that the container for storing data is a linear array.
First, a static internal class Entry is implemented in HashMap. Its important attributes are key, value, next. From the attributes key and value, we can clearly see that Entry is a basic bean for the implementation of HashMap key-value pairs. We As mentioned above, the basis of HashMap is a linear array. This array is Entry[], and the contents of the Map are stored in Entry[].
The HashTable container uses synchronized to ensure thread safety, but the efficiency of HashTable is very low when thread competition is fierce. Because when a thread accesses the synchronization method of HashTable, other threads may enter a blocking or polling state when accessing the synchronization method of HashTable.
The lock segmentation technology used by ConcurrentHashMap first divides the data into segments for storage, and then assigns a lock to each segment of data. When a thread occupies the lock to access one segment of data, the data of other segments can also be accessed. accessed by other threads.
2. Implementation principle of put() and get() of hashmap?
The first key-value pair A comes in, and the index=0 obtained by calculating the hash of its key is recorded as: Entry[0] = A. After a while, another key-value pair B comes in, and its index is equal to 0 by calculation. What should we do now? HashMap will do this: B.next = A, Entry[0] = B. If C comes in again, the index is also equal to 0, then C.next = B, Entry[0] = C; in this way we find the place where index=0 In fact, three key-value pairs A, B, and C are accessed, and they are linked together through the next attribute. So don’t worry if you have any questions. That is to say, the last inserted element is stored in the array.
get() locates first and then traverses.
3. Spring bean life cycle?
In spring, the singleton attribute defaults to true. If it is set to false, a new instance will be generated every time the Bean obtained by the alias is specified.
3.1: Creation of Bean: The container looks for the definition information of the Bean and instantiates it.
3.2: Attribute injection.
3.3: BeanNameAware
3.4: BeanFactoryAware's setBeanFactory()
3.5: BeanPostProcessors' ProcessBeforeInitialization()
3.6: initializingBean's afterPropertiesSet():
3.7: Define init-method in the Bean definition file :
3.8: ProcessaAfterInitialization() of BeanPostProcessors
Destroy() of DisposableBean, destroy-method is defined in the Bean definition file
4. How does spring manage transactions?
Spring’s transaction can be said to be an implementation of Spring AOP. Reflection and dynamic proxies.
AOP aspect-oriented programming means extending the original functions without modifying the source code and operating specific classes through proxy classes.
Spring is a container. Objects are managed through the spring container, and spring manages objects according to the configuration file.
There are two ways to declare transactions in spring, programmatic and declarative. Spring mainly manages transactions through "declarative transactions", that is, it is declared in the configuration file, and the transaction aspect is cut into the program through AOP. The biggest benefit is that the amount of code is greatly reduced.
5. Is the service layer a single instance or multiple instances? Is it thread safe? What if you want to do multiple instances?
The object generated by spring is singleton by default. It can be changed to multiple instances through the scope attribute.
5.1. Servlet is a singleton, strictly speaking It is a ServletMapping that corresponds to a singleton instance
5.2. There are many ways to maintain Servlet thread safety. Usually, synchronized blocks (or methods) are used to protect shared data. Secondly, some lock mechanisms can be volatile and Lock, and ThreadLocal can also be used. Opening up a safe channel, and at the same time blocking the other party's thread for thread safety, its performance is very poor. Do not define variables in the C layer to prevent multi-thread concurrency.
6. The spread of things? Database isolation level?
PROPAGATION_REQUIRED If a transaction exists, the current transaction is supported. Enable if there is no transaction
PROPAGATION_SUPPORTS If there is a transaction, support the current transaction. If there is no transaction, non-transactional execution
PROPAGATION_MANDATORY If a transaction already exists, support the current transaction. If there is no active transaction, an exception is thrown.
PROPAGATION_REQUIRES_NEW Always start a new transaction. If a transaction already exists, the existing transaction is suspended.
PROPAGATION_NOT_SUPPORTED Always execute non-transactionally, suspending any existing transactions.
PROPAGATION_NEVER Always execute non-transactionally, if there is an active transaction, throw an exception
PROPAGATION_NESTED If an active transaction exists, run in a nested transaction. If there is no active transaction,
Then Execute by TransactionDefinition.PROPAGATION_REQUIRED property
By default, we use
PROPAGATION_REQUIRED
① Serializable: It can avoid the occurrence of dirty reads, non-repeatable reads, and phantom reads.
② Repeatable read: It can avoid the occurrence of dirty reads and non-repeatable reads.
③ Read committed: This can avoid the occurrence of dirty reads.
④ Read uncommitted (read uncommitted): the lowest level, cannot be guaranteed under any circumstances.
The default level for general databases is committedRead.
The default isolation level of MySQL is Repeatable read.
View the isolation level of the current transaction in the MySQL database:
select @@tx_isolation;
Set the isolation level of the transaction in the MySQL database:
set [glogal | session] transaction isolation level isolation level name;
set tx_isolation='isolation level name;'
Remember: setting the isolation level of the database must be before starting a transaction!
7. How does Arraylist achieve thread safety?
So in order to solve this thread safety problem you can use Collections.synchronizedList() like this:
List
The first method:
Under Windows, in the file /bin/catalina.bat, under Unix, in front of the file /bin/catalina.sh , add the following settings:
JAVA_OPTS='-Xms [Initialization memory size] -Xmx [Maximum memory that can be used]'
It is necessary to increase the values of these two parameters. For example:
JAVA_OPTS='-Xms256m -Xmx512m'
Indicates that the initial memory is 256MB and the maximum memory that can be used is 512MB.
Second method: Set the environment variable variable name: JAVA_OPTS Variable value: -Xms512m -Xmx512m
Third method: The first two methods are for the case where there is catalina.bat in the bin directory (For example, directly decompressed Tomcat, etc.), but some installed versions of Tomcat do not have catalina.bat. In this case, you can use the following method. Of course, this method is also the most common method: open tomcatHome//bin//tomcat5w.exe, click Java tab, and then you will find that there are two items: Initial memory pool and Maximum memory pool. Initial memory pool is the size of the initialized memory. Maximum memory pool is the maximum memory size. After setting it, press OK and then restart TOMCAT. You will find that the memory available for jvm in tomcat has changed.
Add it under @echo off in catalina.bat (the second line)
set JAVA_OPTS=-server -Xms512m -Xmx1024m -XX:MaxNewSize=512m -XX:MaxPermSize=256m
9. Servlet default implementation class?
lServlet interface SUN Company defines two default implementation classes, namely: GenericServlet and HttpServlet.
If it is a GET request, call the doGet method of HttpServlet. If it is a Post request, call the doPost method. Therefore, when developers write Servlets, they usually only need to override the doGet or doPost method instead of overriding the service method
The above is the detailed content of Java interview questions (must read when looking for a job). For more information, please follow other related articles on the PHP Chinese website!