Home Java JavaInterview questions Java multithreading and concurrency interview questions (1~3 questions, with answers)

Java multithreading and concurrency interview questions (1~3 questions, with answers)

Nov 23, 2019 pm 02:56 PM
java

Java multithreading and concurrency interview questions (1~3 questions, with answers)

1. 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:

// 测试入口函数
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!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undress AI Tool

Undress AI Tool

Undress images for free

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

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

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

PHP Tutorial
1503
276
Comparing Java Frameworks: Spring Boot vs Quarkus vs Micronaut Comparing Java Frameworks: Spring Boot vs Quarkus vs Micronaut Aug 04, 2025 pm 12:48 PM

Pre-formanceTartuptimeMoryusage, Quarkusandmicronautleadduetocompile-Timeprocessingandgraalvsupport, Withquarkusoftenperforminglightbetterine ServerLess scenarios.2.Thyvelopecosyste,

Volume keys on keyboard not working Volume keys on keyboard not working Aug 05, 2025 pm 01:54 PM

First,checkiftheFnkeysettingisinterferingbytryingboththevolumekeyaloneandFn volumekey,thentoggleFnLockwithFn Escifavailable.2.EnterBIOS/UEFIduringbootandenablefunctionkeysordisableHotkeyModetoensurevolumekeysarerecognized.3.Updateorreinstallaudiodriv

How to compare two strings in Java? How to compare two strings in Java? Aug 04, 2025 am 11:03 AM

Use the .equals() method to compare string content, because == only compare object references rather than content; 1. Use .equals() to compare string values equally; 2. Use .equalsIgnoreCase() to compare case ignoring; 3. Use .compareTo() to compare strings in dictionary order, returning 0, negative or positive numbers; 4. Use .compareToIgnoreCase() to compare case ignoring; 5. Use Objects.equals() or safe call method to process null strings to avoid null pointer exceptions. In short, you should avoid using == for string content comparisons unless it is explicitly necessary to check whether the object is in phase.

Computed properties vs methods in Vue Computed properties vs methods in Vue Aug 05, 2025 am 05:21 AM

Computed has a cache, and multiple accesses are not recalculated when the dependency remains unchanged, while methods are executed every time they are called; 2.computed is suitable for calculations based on responsive data. Methods are suitable for scenarios where parameters are required or frequent calls but the result does not depend on responsive data; 3.computed supports getters and setters, which can realize two-way synchronization of data, but methods are not supported; 4. Summary: Use computed first to improve performance, and use methods when passing parameters, performing operations or avoiding cache, following the principle of "if you can use computed, you don't use methods".

How to join an array of strings in Java? How to join an array of strings in Java? Aug 04, 2025 pm 12:55 PM

Using String.join() (Java8) is the easiest recommended method for connecting string arrays, just specify the separator directly; 2. For old versions of Java or when more control is needed, you can use StringBuilder to manually traverse and splice; 3. StringJoiner is suitable for scenarios that require more flexible formats such as prefixes and suffixes; 4. Using Arrays.stream() combined with Collectors.joining() is suitable for filtering or converting the array before joining; To sum up, if Java8 and above is used, the String.join() method should be preferred in most cases, which is concise and easy to read, but for complex logic, it is recommended.

python logging to file example python logging to file example Aug 04, 2025 pm 01:37 PM

Python's logging module can write logs to files through FileHandler. First, call the basicConfig configuration file processor and format, such as setting the level to INFO, using FileHandler to write app.log; secondly, add StreamHandler to achieve output to the console at the same time; Advanced scenarios can use TimedRotatingFileHandler to divide logs by time, for example, setting when='midnight' to generate new files every day and keep 7 days of backup, and make sure that the log directory exists; it is recommended to use getLogger(__name__) to create named loggers, and produce

python pandas styling dataframe example python pandas styling dataframe example Aug 04, 2025 pm 01:43 PM

Using PandasStyling in JupyterNotebook can achieve the beautiful display of DataFrame. 1. Use highlight_max and highlight_min to highlight the maximum value (green) and minimum value (red) of each column; 2. Add gradient background color (such as Blues or Reds) to the numeric column through background_gradient to visually display the data size; 3. Custom function color_score combined with applymap to set text colors for different fractional intervals (≥90 green, 80~89 orange, 60~79 red,

Advanced Conditional Types in TypeScript Advanced Conditional Types in TypeScript Aug 04, 2025 am 06:32 AM

TypeScript's advanced condition types implement logical judgment between types through TextendsU?X:Y syntax. Its core capabilities are reflected in the distributed condition types, infer type inference and the construction of complex type tools. 1. The conditional type is distributed in the bare type parameters and can automatically split the joint type, such as ToArray to obtain string[]|number[]. 2. Use distribution to build filtering and extraction tools: Exclude excludes types through TextendsU?never:T, Extract extracts commonalities through TextendsU?T:Never, and NonNullable filters null/undefined. 3

See all articles