


Java multithreading and concurrency interview questions (1~3 questions, with answers)
Nov 23, 2019 pm 02:56 PM1. DelayQueue delayed unbounded blocking queue
When talking about the use and principle of DelayQueue, we first introduce DelayQueue, DelayQueue Is an unbounded blocking queue from which elements can be extracted only when the delay expires. The head of the queue is the Delayed element that will be stored the longest after the delay expires. (Recommended study: java interview questions)
DelayQueue blocking queue is also often used in our system development, for example: the design of the cache system, the objects in the cache, exceed the idle time , needs to be moved from the cache; the task scheduling system can accurately grasp the execution time of the task. We may need to process a lot of time-critical data through threads.
If we use ordinary threads, we need to traverse all the objects and check one by one to see if the data has expired, etc. First of all, the execution efficiency will not be too high, and secondly, the design style is also Greatly affects the accuracy of the data. A task that needs to be executed at 12:00 may not be executed until 12:01, which has greater disadvantages for systems with high data requirements. From this we can use DelayQueue.
The following will give an introduction to DelayQueue, and then give an example. And provide an implementation of the Delayed interface and Sample code. DelayQueue is a BlockingQueue whose specialized parameter is Delayed.
(Students who don’t know BlockingQueue, please learn about BlockingQueue first and then read this article) Delayed extends the Comparable interface. The comparison benchmark is the delay time value. The return value of getDelay, the implementation class of the Delayed interface, should be a fixed value. (final). DelayQueue is internally implemented using PriorityQueue.
DelayQueue=BlockingQueue+PriorityQueue+Delayed
The key elements of DelayQueue are BlockingQueue, PriorityQueue, and Delayed. It can be said that DelayQueue is a BlockingQueue implemented using a priority queue (PriorityQueue). The comparison base value of the priority queue is time.
Their basic definitions are as follows
public interface Comparable<T> { public int compareTo(T o); } public interface Delayed extends Comparable<Delayed> { long getDelay(TimeUnit unit); } public class DelayQueue<E extends Delayed> implements BlockingQueue<E> { private final PriorityQueue<E> q = new PriorityQueue<E>(); }
The internal implementation of DelayQueue uses a priority queue. When the offer method of DelayQueue is called, the Delayed object is added to the priority queue q. As follows:
public boolean offer(E e) { final ReentrantLock lock = this.lock; lock.lock(); try { E first = q.peek(); q.offer(e); if (first == null || e.compareTo(first) < 0) available.signalAll(); return true; } finally { lock.unlock(); } }
The take method of DelayQueue takes out the first of the priority queue q (peek). If the delay threshold is not reached, await processing is performed. As follows:
public E take() throws InterruptedException { final ReentrantLock lock = this.lock; lock.lockInterruptibly(); try { for (; ; ) { E first = q.peek(); if (first == null) { available.await(); } else { long delay = first.getDelay(TimeUnit.NANOSECONDS); if (delay > 0) { long tl = available.awaitNanos(delay); } else { E x = q.poll(); assert x != null; if (q.size() != 0) available.signalAll(); //wake up other takers return x; } } } } finally { lock.unlock(); } }
DelayQueue instance application
Ps: In order to have calling behavior, the elements stored in DelayDeque must inherit the Delayed interface. The Delayed interface makes the object a delayed object, which gives the object stored in the DelayQueue class an activation date. This interface enforces the following two methods.
The following will use Delay to implement a cache. It includes a total of three classes: Pair, DelayItem, and Cache
Pair class:
public class Pair<K, V> { public K first; public V second; public Pair() { } public Pair(K first, V second) { this.first = first; this.second = second; } }
The following is the implementation of the Delay interface:
import java.util.concurrent.Delayed; import java.util.concurrent.TimeUnit; import java.util.concurrent.atomic.AtomicLong; public class DelayItem<T> implements Delayed { /** * Base of nanosecond timings, to avoid wrapping */ private static final long NANO_ORIGIN = System.nanoTime(); /** * Returns nanosecond time offset by origin */ final static long now() { return System.nanoTime() - NANO_ORIGIN; } /** * Sequence number to break scheduling ties, and in turn to guarantee FIFO order among tied * entries. */ private static final AtomicLong sequencer = new AtomicLong(0); /** * Sequence number to break ties FIFO */ private final long sequenceNumber; /** * The time the task is enabled to execute in nanoTime units */ private final long time; private final T item; public DelayItem(T submit, long timeout) { this.time = now() + timeout; this.item = submit; this.sequenceNumber = sequencer.getAndIncrement(); } public T getItem() { return this.item; } public long getDelay(TimeUnit unit) { long d = unit.convert(time - now(), TimeUnit.NANOSECONDS); return d; } public int compareTo(Delayed other) { if (other == this) // compare zero ONLY if same object return 0; if (other instanceof DelayItem) { DelayItem x = (DelayItem) other; long diff = time - x.time; if (diff < 0) return -1; else if (diff > 0) return 1; else if (sequenceNumber < x.sequenceNumber) return -1; else return 1; } long d = (getDelay(TimeUnit.NANOSECONDS) - other.getDelay(TimeUnit.NANOSECONDS)); return (d == 0) ?0 :((d < 0) ?-1 :1); } }
The following is the implementation of Cache, including put and get methods
import javafx.util.Pair; import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ConcurrentMap; import java.util.concurrent.DelayQueue; import java.util.concurrent.TimeUnit; import java.util.logging.Level; import java.util.logging.Logger; public class Cache<K, V> { private static final Logger LOG = Logger.getLogger(Cache.class.getName()); private ConcurrentMap<K, V> cacheObjMap = new ConcurrentHashMap<K, V>(); private DelayQueue<DelayItem<Pair<K, V>>> q = new DelayQueue<DelayItem<Pair<K, V>>>(); private Thread daemonThread; public Cache() { Runnable daemonTask = new Runnable() { public void run() { daemonCheck(); } }; daemonThread = new Thread(daemonTask); daemonThread.setDaemon(true); daemonThread.setName("Cache Daemon"); daemonThread.start(); } private void daemonCheck() { if (LOG.isLoggable(Level.INFO)) LOG.info("cache service started."); for (; ; ) { try { DelayItem<Pair<K, V>> delayItem = q.take(); if (delayItem != null) { // 超時對象處理 Pair<K, V> pair = delayItem.getItem(); cacheObjMap.remove(pair.first, pair.second); // compare and remove } } catch (InterruptedException e) { if (LOG.isLoggable(Level.SEVERE)) LOG.log(Level.SEVERE, e.getMessage(), e); break; } } if (LOG.isLoggable(Level.INFO)) LOG.info("cache service stopped."); } // 添加緩存對象 public void put(K key, V value, long time, TimeUnit unit) { V oldValue = cacheObjMap.put(key, value); if (oldValue != null) q.remove(key); long nanoTime = TimeUnit.NANOSECONDS.convert(time, unit); q.put(new DelayItem<Pair<K, V>>(new Pair<K, V>(key, value), nanoTime)); } public V get(K key) { return cacheObjMap.get(key); } }
Test the main method:
// 測試入口函數(shù) public static void main(String[] args) throws Exception { Cache<Integer, String> cache = new Cache<Integer, String>(); cache.put(1, "aaaa", 3, TimeUnit.SECONDS); Thread.sleep(1000 * 2); { String str = cache.get(1); System.out.println(str); } Thread.sleep(1000 * 2); { String str = cache.get(1); System.out.println(str); } }
The output result is:
aaaa null
We see the above result. If the delay time is exceeded, the data in the cache will be automatically lost, and the result will be null.
2. Concurrent (Collection) queue-non-blocking queue
Non-blocking queue
First we need to briefly understand Next, what is a non-blocking queue:
Contrary to the blocking queue, the execution of the non-blocking queue will not be blocked, whether it is the dequeuing of consumers or the joining of producers. Under the hood, non-blocking queues use CAS (compare and swap) to achieve non-blocking thread execution.
Simple operation of non-blocking queue
The same as blocking queue, the common methods in non-blocking queue are dequeue and enqueue.
offer(): A method inherited from the Queue interface, which implements the queue entry operation without hindering the execution of the thread. If the insertion is successful, it returns true; Dequeue method:
poll(): Move the head node pointer, return the head node element, and dequeue the head node element; if the queue is empty, return null;
peek(): Move the head node pointer, return the head node element , the head node element will not be dequeued; if the queue is empty, null will be returned;
3. Non-blocking algorithm CAS
First we need Understand pessimistic locking and optimistic locking
Pessimistic lock: Assume that the concurrency environment is pessimistic. If a concurrency conflict occurs, consistency will be destroyed, so conflicts must be completely prohibited through exclusive locks. There is a classic metaphor, "If you don't lock the door, then the troublemaker will break in and make a mess", so "you can only open the door and let in one person at a time, so you can keep an eye on him."
Optimistic locking: Assume that the concurrency environment is optimistic, that is, although there will be concurrency conflicts, the conflicts can be found and will not cause damage. Therefore, you can not add any protection, and then decide to give up the operation or try again after discovering the concurrency conflicts. . An analogy is, "If you don't lock the door, then although the troublemakers will break in, they will know if they want to destroy you." Therefore, "You can just let everyone in and wait until you find out that they want to destroy." Make a decision".
It is generally believed that the performance of optimistic locking is higher than that of pessimistic locking, especially in some complex scenarios. This is mainly because pessimistic locks will also protect certain operations that will not cause damage while locking; while competition for optimistic locks only occurs at the smallest concurrency conflicts. If we use pessimistic locks to understand it, it is "lock The granularity is the smallest”. However, the design of optimistic locks is often more complex, so pessimistic locks are often used in complex scenarios. Ensure correctness first, and then pursue performance if necessary.
The implementation of optimistic locking often requires hardware support. Most processors implement a CAS instruction to implement the semantics of "Compare And Swap" (swap here means "swapping in", that is, set), forming a basic optimistic lock. CAS contains 3 operands:
The memory location to be read and written V
The value to be compared A
The new value to be written B
If and only if the value of position V is equal to A, CAS will atomically update the value of position V with the new value B; otherwise no operation will be performed. Regardless of whether the value of position V is equal to A, the original value of V will be returned. An interesting fact is that "using CAS to control concurrency" is not equivalent to "using optimistic locking". CAS is just a means to achieve both optimistic locking and pessimistic locking. Optimism and pessimism are just concurrency control strategies.
The above is the detailed content of Java multithreading and concurrency interview questions (1~3 questions, with answers). For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undress AI Tool
Undress images for free

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

There are three common methods to traverse Map in Java: 1. Use entrySet to obtain keys and values at the same time, which is suitable for most scenarios; 2. Use keySet or values to traverse keys or values respectively; 3. Use Java8's forEach to simplify the code structure. entrySet returns a Set set containing all key-value pairs, and each loop gets the Map.Entry object, suitable for frequent access to keys and values; if only keys or values are required, you can call keySet() or values() respectively, or you can get the value through map.get(key) when traversing the keys; Java 8 can use forEach((key,value)->

Optional can clearly express intentions and reduce code noise for null judgments. 1. Optional.ofNullable is a common way to deal with null objects. For example, when taking values ??from maps, orElse can be used to provide default values, so that the logic is clearer and concise; 2. Use chain calls maps to achieve nested values ??to safely avoid NPE, and automatically terminate if any link is null and return the default value; 3. Filter can be used for conditional filtering, and subsequent operations will continue to be performed only if the conditions are met, otherwise it will jump directly to orElse, which is suitable for lightweight business judgment; 4. It is not recommended to overuse Optional, such as basic types or simple logic, which will increase complexity, and some scenarios will directly return to nu.

In Java, Comparable is used to define default sorting rules internally, and Comparator is used to define multiple sorting logic externally. 1.Comparable is an interface implemented by the class itself. It defines the natural order by rewriting the compareTo() method. It is suitable for classes with fixed and most commonly used sorting methods, such as String or Integer. 2. Comparator is an externally defined functional interface, implemented through the compare() method, suitable for situations where multiple sorting methods are required for the same class, the class source code cannot be modified, or the sorting logic is often changed. The difference between the two is that Comparable can only define a sorting logic and needs to modify the class itself, while Compar

The core workaround for encountering java.io.NotSerializableException is to ensure that all classes that need to be serialized implement the Serializable interface and check the serialization support of nested objects. 1. Add implementsSerializable to the main class; 2. Ensure that the corresponding classes of custom fields in the class also implement Serializable; 3. Use transient to mark fields that do not need to be serialized; 4. Check the non-serialized types in collections or nested objects; 5. Check which class does not implement the interface; 6. Consider replacement design for classes that cannot be modified, such as saving key data or using serializable intermediate structures; 7. Consider modifying

To deal with character encoding problems in Java, the key is to clearly specify the encoding used at each step. 1. Always specify encoding when reading and writing text, use InputStreamReader and OutputStreamWriter and pass in an explicit character set to avoid relying on system default encoding. 2. Make sure both ends are consistent when processing strings on the network boundary, set the correct Content-Type header and explicitly specify the encoding with the library. 3. Use String.getBytes() and newString(byte[]) with caution, and always manually specify StandardCharsets.UTF_8 to avoid data corruption caused by platform differences. In short, by

Method reference is a way to simplify the writing of Lambda expressions in Java, making the code more concise. It is not a new syntax, but a shortcut to Lambda expressions introduced by Java 8, suitable for the context of functional interfaces. The core is to use existing methods directly as implementations of functional interfaces. For example, System.out::println is equivalent to s->System.out.println(s). There are four main forms of method reference: 1. Static method reference (ClassName::staticMethodName); 2. Instance method reference (binding to a specific object, instance::methodName); 3.

JavaScript data types are divided into primitive types and reference types. Primitive types include string, number, boolean, null, undefined, and symbol. The values are immutable and copies are copied when assigning values, so they do not affect each other; reference types such as objects, arrays and functions store memory addresses, and variables pointing to the same object will affect each other. Typeof and instanceof can be used to determine types, but pay attention to the historical issues of typeofnull. Understanding these two types of differences can help write more stable and reliable code.

There are three common ways to parse JSON in Java: use Jackson, Gson, or org.json. 1. Jackson is suitable for most projects, with good performance and comprehensive functions, and supports conversion and annotation mapping between objects and JSON strings; 2. Gson is more suitable for Android projects or lightweight needs, and is simple to use but slightly inferior in handling complex structures and high-performance scenarios; 3.org.json is suitable for simple tasks or small scripts, and is not recommended for large projects because of its lack of flexibility and type safety. The choice should be decided based on actual needs.
