Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What are the back-end test questions of JAVA?

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly explains "what are the back-end test questions of JAVA". Interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Next, let the editor take you to learn "what are the back-end test questions of JAVA"?

1. Similarities and differences between synchronized and reentrantlock

Identical point

Both implement multi-thread synchronization and memory visibility semantics.

All are reentrant locks.

Differences

Different implementation mechanisms: synchronized implements reentrantlock through java object headlock tags and Monitor objects through CAS, ASQ (AbstractQueuedSynchronizer) and locksupport (for blocking and unblocking) synchronized dependent jvm memory model ensures multithreaded memory visibility containing shared variables reentrantlock ensures multithreaded memory visibility containing shared variables through ASQ's volatile state

Different ways of using synchronized can modify instance method (lock instance object), static method (lock class object), code block (display specified lock object) reentrantlock display call trylock () / lock () method, need to release the lock in the finally block

Reentrantlock provides rich semantic reentrantlock such as limited time waiting lock (setting expiration time), interruptible lock (lockInterruptibly), condition (providing await, signal, etc.). Reentrantlock provides fair lock and unfair lock to realize synchronized unsettable wait time and uninterruptible (interrupted)

2. Why concurrenthashmap does not need to be locked when reading

Jdk1.7

1) key, hash and next in HashEntry are all final type, and only the header can be inserted or deleted.

2) the value field of the HashEntry class is declared to be of type volly

3) null is not allowed to be used as the key and value. When the reading thread reads that the value of the value field of a HashEntry is null, it knows that there is a conflict-- reordering occurs (put sets the bytecode instruction for the new value object to reorder), and the value needs to be re-read after locking.

4) the volatile variable count coordinates the memory visibility between the read and write threads. The count is modified after the write operation, and the read operation reads count first. According to the principle of happen-before transitivity, the modified read operation of the write operation can be seen.

Jdk1.8

1) both val and next of Node are volley type.

2) the unsafe operation corresponding to tabAt and casTabAt implements volatile semantics.

3. The role of ContextClassLoader (thread context class loader)

Load the class beyond the parent delegation mechanism of the class loader, such as the serviceloader implementation

Using thread context class loaders to load classes, be careful to ensure that the class loaders between multiple threads that need to communicate should be the same to prevent type conversion exceptions (ClassCastException) due to different class loaders

4. Tomcat class loading mechanism

Different applications use different webapp class loaders to achieve application isolation. Below the webapp class loader is the jsp class loader.

Jar packages shared by different applications can be placed in the Shared classloader / shared directory

5. Osgi class loading mechanism

The osgi class loading model is netted and can be delegated between modules (Bundle)

The key to the realization of modular hot deployment in osgi is the implementation of custom class loader mechanism. Each Bundle has its own class loader. When you need to replace a Bundle, replace Bundle with similar loaders to achieve hot replacement of code.

When a class load request is received, osgi performs a class search in the following order:

1) delegate the class starting with java.* to the parent class loader to load

2) otherwise, delegate the class in the delegate list (defined in the configuration file org.osgi.framework.bootdelegation) to the parent class loader to load

3) otherwise, check whether it is declared in Import-Package, and if so, load it by the class loader delegated to the Bundle of the class Export

4) otherwise, check whether it is declared in Require-Bundle, and if so, delegate the class load request to required bundle's class loader

5) otherwise, find the ClassPath of the current Bundle and load it with your own class loader

6) otherwise, find out whether the class is in its own Fragment Bundle, and if so, load it to the class loader delegated to Fragment Bundle

7) otherwise, look for the Bundle of the Dynamic Import-Package (Dynamic Import loads only when the Package is actually used) and delegate it to the classloader of the corresponding Bundle to load

8) otherwise, the class lookup fails

6. Similarities and differences between sleep and wait

Wait needs to be combined with synchronized. The synchronized lock will be released when wait is used.

Sleep will only hand over the cpu, not the lock

Both are likely to be interrupt

7. How to end a thread that is running all the time

Using the exit flag, this flag variable is visible to multiple threads

Use interrupt, in combination with isInterrupted ()

8. Threadlocal usage scenarios and problems

Threadlocal can not solve the problem of multi-thread sharing variables. Objects contained in the same threadlocal have different copies in different thread and do not interfere with each other.

It is used to store thread context variables, and it is convenient for the same thread to read variables before and after multiple times, such as transactions and database connection connections. It is more used in web programming.

Problem: note that the thread pool scenario uses threadlocal, because the actual variable value is stored in the threadlocalmap type variable of thread. If the value does not have remove or prior set, you may get the previous old value.

Problem: pay attention to the memory leak in the thread pool scenario. Although threadlocal's get/set clears key (key is a weak reference to threadlocal, value is a strong reference, causing value not to release) entry that is null, it is best to remove

9. The process of thread pool from startup to work

When it was first created, there were no threads in it

When you call execute () to add a task:

1) if the number of running threads is less than the core parameter corePoolSize, continue to create threads to run the task

2) otherwise, if the number of running threads is greater than or equal to corePoolSize, add the task to the blocking queue

3) otherwise, if the queue is full and the number of running threads is less than the core parameter maximumPoolSize, continue to create threads to run the task

4) otherwise, if the queue is full and the number of running threads is greater than or equal to maximumPoolSize, it will be handled according to the set rejection policy.

5) complete one task and continue to process the next task

6) when there is no task to continue processing, the thread is interrupted or the thread pool is closed, the thread exits execution, and if the thread pool is closed, the thread ends

7) otherwise, determine whether the number of threads running in the thread pool is greater than the number of core threads, and if so, the thread ends, otherwise the thread blocks. Therefore, after all the thread pool tasks have been executed, the thread pool size that remains is corePoolSize.

10. Difference between blocking queue BlockingQueue take and poll

Poll (time): take the object that ranks first in the BlockingQueue. If it cannot be removed immediately, you can wait for the time specified in the time parameter, and return null if it is not available.

Take (): take the first object in the BlockingQueue. If BlockingQueue is empty, block until a new object is added to the BlockingQueue

11. How to get the result from FutureTask without blocking

Get (long timeout,TimeUnit unit), return if timeout occurs

Polling, first determine whether it is finished by isDone (), and then call get ()

12. If blockingqueue stores critical data, how to deal with system downtime

Open-ended questions, welcome to discuss

It is troublesome to persist the queue. You need to persist the production data to disk and return after the persistence is successful. The consumer thread loads data from disk to memory blocking queue, maintains consumption offset, and loads data from disk according to consumption offset when starting.

Join the message queue to ensure that messages are not lost, generate serial numbers, consume idempotents, and determine the production status after the system is restarted according to the consumption process.

13. The difference between NIO and traditional Igamo

Save threads, NIO from the original each thread needs to block read and write to a single thread (that is, Selector) is responsible for handling multiple channel registrations (register) SelectionKey collection (the underlying epoll () provided by the operating system), netty bossgroup handles accept connections (do not understand why bossgroup sets up multiple thread necessity), workergroup handles specific business processes and data read and write

NIO provides non-blocking operation

Traditional NIO O processes data in stream, while NIO processes data in blocks. NIO provides bytebuffer, which is divided into in-heap and out-of-heap buffers, which are first put into this buffer when reading and writing, and then transferred by the kernel to the peer through channel. The out-of-heap buffer does not leave the kernel, which improves performance.

14. Storing repeatable strings in list, how to delete a string

Call iterator related methods to delete

Reverse deletion to prevent array rearrangement caused by positive sequence deletion. Index skips array element problems.

15. What are the GC ROOTS (memory leaks associated with this are more relevant to daily development)

All Java threads point to references to objects in the GC heap in the currently active stack frame, so objects that are not needed are set to null in time to improve memory recovery efficiency.

Objects referenced by static variables, thus reducing the size of static variables, especially static collection variables, and overriding euqls () and hashcode () for objects stored in the collection to prevent continuous growth

Objects referenced by the local method JNI

Objects referenced by constants in the method area, thus reducing calls to String.intern () on long strings

Class objects loaded by classloader, so set null in time when custom classloader is invalid and pay attention to isolation between objects loaded by class loaders

References to objects in the GC heap in some static data structures in jvm

At this point, I believe you have a deeper understanding of "what are the JAVA back-end test questions?" you might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report