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 interview questions often asked by JAVA multithreading?

2025-03-29 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 interview questions often asked by JAVA multithreading". The explanation in the article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "what are the interview questions frequently asked by JAVA multithreading"?

What is the difference between synchronized and ReentrantLock in Java?

For a long time in the past, Java can only be mutually exclusive through the synchronized keyword, but it has some disadvantages. For example, you can't extend methods or block boundaries outside the lock, and you can't cancel when you try to acquire the lock. Java 5 provides more complex control to solve these problems through the Lock interface. The ReentrantLock class implements Lock, which has the same concurrency and memory semantics as synchronized, and it is also extensible.

There are three threads, T1 and T2, T3. How do you make sure they are executed sequentially?

In multithreading, there are several ways for threads to execute in a specific order. You can use the thread class's join () method to start another thread in one thread, and another thread finishes the thread to continue execution. To ensure the order of the three threads, you should start the last one first (T3 call T2 T2 call T1) so that T1 completes first and T3 finishes last.

What is the purpose of the yield method in the Thread class?

The Yield method pauses the currently executing thread object to allow other threads with the same priority to execute. It is a static method and only guarantees that the current thread abandons the CPU occupation, but there is no guarantee that other threads must be able to occupy the CPU. The thread that executes yield () may be executed as soon as it enters the paused state.

What is the concurrency of ConcurrentHashMap in Java?

ConcurrentHashMap divides the actual map into several parts to achieve its extensibility and thread safety. This partition is obtained using concurrency, which is an optional parameter for the ConcurrentHashMap class constructor with a default value of 16, so that contention can be avoided in the case of multithreading.

What is the difference between the submit () and execute () methods in the Java thread pool?

Both methods can submit tasks to the thread pool. The return type of the execute () method is void, which is defined in the Executor interface, while the submit () method can return the Future object that holds the calculation results. It is defined in the ExecutorService interface, which extends the Executor interface, and other thread pool classes such as ThreadPoolExecutor and ScheduledThreadPoolExecutor have these methods.

What is the blocking method?

The blocking method means that the program waits for the method to complete and does nothing else, while the accept () method of ServerSocket waits for the client to connect. Blocking here means that the current thread is suspended until the result of the call is returned and will not return until the result is obtained. In addition, there are asynchronous and non-blocking methods that return before the task is completed.

What is ReadWriteLock in Java?

Generally speaking, read-write lock is the result of lock separation technology used to improve the performance of concurrent programs. ReadWriteLock in Java is a new interface in Java 5. One ReadWriteLock maintains a pair of associated locks, one for read-only operations and one for writing. In the absence of a writer thread, a read lock may be held by multiple reader threads at the same time. Write locks are exclusive, and you can implement this rule using ReentrantReadWriteLock in JDK, which supports up to 65535 write locks and 65535 read locks.

What is a busy loop in multithreading?

A busy loop is a programmer who uses a loop to make a thread wait, unlike the traditional methods wait (), sleep () or yield (), which all give up CPU control, while a busy loop does not give up CPU, it is running an empty loop. The goal is to preserve the CPU cache, which may be rebuilt when a waiting thread wakes up in another kernel in a multicore system. It can be used to avoid rebuilding the cache and reduce the time it takes to wait for a rebuild.

What is the difference between the volatile variable and the atomic variable?

First of all, the volatile variable looks very similar to the atomic variable, but the function is different. The Volatile variable ensures antecedent relationships, that is, writes occur before subsequent reads, but it does not guarantee atomicity. For example, modify the count variable with volatile, then the count++ operation is not atomic. The atomic method provided by the AtomicInteger class can make this operation atomic, such as the getAndIncrement () method atomically increments the current value to one, and other data types and reference variables can do similar operations.

Write 3 multithreading best practices that you follow

Here are three best practices that I think most Java programmers should follow:

Give your thread a meaningful name.

This makes it easy to find bug or track it. The names OrderProcessor and QuoteProcessor or TradeProcessor are better than Thread-1. Thread-2 and Thread-3 is much better, giving the thread a name related to the task it is going to accomplish, and all major frameworks and even JDK follow this best practice.

Avoid locking and narrowing synchronization

Locks are expensive and context switching takes more time and space. Try minimizing the use of synchronization and locks to narrow the critical area. So I prefer synchronization blocks to synchronization methods, which give me absolute control over locks.

Multi-purpose synchronization classes use less wait and notify

First of all, synchronization classes such as CountDownLatch, Semaphore, CyclicBarrier and Exchanger simplify the coding operation, but it is difficult to control the complex control flow with wait and notify. Second, these classes are written and maintained by the best enterprises and will be continuously optimized and refined in subsequent JDK, and your program can be optimized effortlessly with these higher-level synchronization tools.

Use more concurrent sets and less synchronous sets

This is another easy-to-follow and highly beneficial best practice. Concurrent collections are more scalable than synchronous collections, so using concurrent collections works better when programming concurrently. If you need to use map next time, you should think of using ConcurrentHashMap first.

How do I force a thread to start?

There is no way to force a thread to start in Java, it is controlled by the thread scheduler and the Java does not publish the relevant API.

What is the fork join framework in Java?

The fork join framework is an efficient tool in JDK7 that allows Java developers to take full advantage of multiprocessors on modern servers. It is specifically designed for those that can be recursively divided into many sub-modules, with the aim of using all available processing power to improve the performance of the program. One of the great advantages of the fork join framework is that it uses the work theft algorithm, and worker threads that can accomplish more tasks can steal tasks from other threads to execute.

Thank you for your reading, these are the contents of "what are the interview questions often asked by JAVA multithreading?" after the study of this article, I believe you have a deeper understanding of what the interview questions frequently asked by JAVA multithreading are, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

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