自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

六種延遲隊(duì)列的實(shí)現(xiàn)方式,你知道幾種?

開發(fā) 前端
DelayQueue? 是 Java 并發(fā)包 java.util.concurrent? 下的一個(gè)線程安全的阻塞隊(duì)列,它存儲(chǔ)的元素必須實(shí)現(xiàn) Delayed 接口,以便計(jì)算元素的延時(shí)時(shí)間。隊(duì)列中的元素只有在其指定的延遲時(shí)間到達(dá)之后才能從隊(duì)列中取出。

1. DelayQueue 延時(shí)隊(duì)列

DelayQueue 是 Java 并發(fā)包 java.util.concurrent 下的一個(gè)線程安全的阻塞隊(duì)列,它存儲(chǔ)的元素必須實(shí)現(xiàn) Delayed 接口,以便計(jì)算元素的延時(shí)時(shí)間。隊(duì)列中的元素只有在其指定的延遲時(shí)間到達(dá)之后才能從隊(duì)列中取出。

import java.util.concurrent.DelayQueue;
import java.util.concurrent.Delayed;
import java.util.concurrent.TimeUnit;

public class Order implements Delayed {
    private long time;
    private String name;

    public Order(String name, long delay, TimeUnit unit) {
        this.name = name;
        this.time = System.currentTimeMillis() + unit.toMillis(delay);
    }

    @Override
    public long getDelay(TimeUnit unit) {
        long delay = time - System.currentTimeMillis();
        return unit.convert(delay, TimeUnit.MILLISECONDS);
    }

    @Override
    public int compareTo(Delayed other) {
        if (this.time < ((Order) other).time) {
            return -1;
        } else if (this.time > ((Order) other).time) {
            return 1;
        }
        return 0;
    }
}

public class DelayQueueDemo {
    public static void main(String[] args) throws InterruptedException {
        DelayQueue<Order> delayQueue = new DelayQueue<>();
        delayQueue.put(new Order("Order1", 5, TimeUnit.SECONDS));
        delayQueue.put(new Order("Order2", 10, TimeUnit.SECONDS));
        delayQueue.put(new Order("Order3", 15, TimeUnit.SECONDS));

        System.out.println("訂單延遲隊(duì)列開始時(shí)間:" + java.time.LocalDateTime.now());
        while (delayQueue.size() != 0) {
            Order order = delayQueue.take(); // 阻塞直到元素可用
            System.out.format("訂單: %s 被取消, 取消時(shí)間: %s\n", order.name, java.time.LocalDateTime.now());
        }
    }
}

2. Quartz 定時(shí)任務(wù)

Quartz 是一個(gè)開源的任務(wù)調(diào)度庫,可以集成到幾乎任何Java應(yīng)用中,用于定時(shí)執(zhí)行任務(wù)。通過定義任務(wù)和觸發(fā)器,可以很容易地實(shí)現(xiàn)定時(shí)任務(wù)。

import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.scheduling.quartz.QuartzJobBean;

public class QuartzJob extends QuartzJobBean {
    @Override
    protected void executeInternal(JobExecutionContext context) throws JobExecutionException {
        System.out.println("執(zhí)行定時(shí)任務(wù): " + System.currentTimeMillis());
    }
}

// 在 Spring 配置文件中配置 Quartz
// ...

3. Redis sorted set

Redis 的有序集合(sorted set)可以利用 score 來實(shí)現(xiàn)延時(shí)隊(duì)列。通過設(shè)置元素的 score 為過期時(shí)間戳,可以實(shí)現(xiàn)在特定時(shí)間自動(dòng)過期并被消費(fèi)。

import redis.clients.jedis.Jedis;

public class RedisDelayQueue {
    private static final String DELAY_QUEUE = "delayQueue";

    public void addToQueue(String key, long delaySeconds) {
        double score = System.currentTimeMillis() / 1000 + delaySeconds;
        new Jedis().zadd(DELAY_QUEUE, score, key);
    }

    public void consume() {
        long now = System.currentTimeMillis() / 1000;
        while (true) {
            Set<String> keys = new Jedis().zrangeByScore(DELAY_QUEUE, 0, now);
            for (String key : keys) {
                new Jedis().zrem(DELAY_QUEUE, key);
                System.out.println("消費(fèi)元素: " + key);
            }
            if (keys.isEmpty()) {
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}

4. Redis 過期回調(diào)

Redis 可以配置過期事件通知,當(dāng)一個(gè)鍵過期時(shí),Redis 會(huì)發(fā)送一個(gè)事件通知給訂閱了該事件的客戶端。

import org.springframework.data.redis.connection.Message;
import org.springframework.data.redis.connection.MessageListener;
import org.springframework.data.redis.listener.ChannelTopic;
import org.springframework.data.redis.listener.RedisMessageListenerContainer;
import org.springframework.data.redis.listener.adapter.MessageListenerAdapter;

public class RedisKeyExpirationListener implements MessageListener {
    @Override
    public void onMessage(Message message, byte[] pattern) {
        String expiredKey = new String(message.getBody());
        System.out.println("監(jiān)聽到key:" + expiredKey + "已過期");
    }
}

// 在 Spring 配置中配置 RedisMessageListenerContainer
// ...

5. RabbitMQ 延時(shí)隊(duì)列

RabbitMQ 通過消息的 TTL(Time To Live)和死信交換機(jī)(DLX)來實(shí)現(xiàn)延時(shí)隊(duì)列。

import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.amqp.core.Message;

public class RabbitMQDelayQueue {
    private final RabbitTemplate rabbitTemplate;

    public RabbitMQDelayQueue(RabbitTemplate rabbitTemplate) {
        this.rabbitTemplate = rabbitTemplate;
    }

    public void sendDelayedMessage(String message, long delay) {
        Message msg = new Message(message.getBytes(), new MessageProperties() {
            {
                setExpiration(String.valueOf(delay));
                // 設(shè)置消息的其他屬性
            }
        });
        rabbitTemplate.send("delayQueueExchange", "delayQueueRoutingKey", msg);
    }

    // 配置交換機(jī)、隊(duì)列和綁定
    // ...
}

6. 時(shí)間輪算法

時(shí)間輪算法是一種高效的定時(shí)任務(wù)管理算法,Netty 提供了 HashedWheelTimer 來實(shí)現(xiàn)時(shí)間輪。

import io.netty.util.HashedWheelTimer;
import io.netty.util.Timer;
import io.netty.util.TimerTask;

public class NettyDelayQueue {
    public static void main(String[] args) {
        Timer timer = new HashedWheelTimer();
        timer.newTimeout(new TimerTask() {
            @Override
            public void run(Timeout timeout) throws Exception {
                System.out.println("任務(wù)執(zhí)行: " + System.currentTimeMillis());
            }
        }, 5, TimeUnit.SECONDS);
    }
}
責(zé)任編輯:武曉燕 來源: 程序猿技術(shù)充電站
相關(guān)推薦

2023-10-30 11:53:37

繼承JS父類

2022-03-28 20:57:31

私有屬性class屬性和方法

2019-09-02 11:14:08

隔離虛擬機(jī)操作系統(tǒng)

2025-04-17 07:10:03

API架構(gòu)項(xiàng)目

2021-12-15 23:10:34

JS Debugger 前端開發(fā)

2021-12-08 10:47:35

RabbitMQ 實(shí)現(xiàn)延遲

2017-06-26 10:35:58

前端JavaScript繼承方式

2018-08-03 16:40:06

前端前端框架微服務(wù)

2023-05-10 13:58:13

服務(wù)限流系統(tǒng)

2018-07-04 11:02:23

無線傳輸模式

2019-05-16 13:00:18

異步編程JavaScript回調(diào)函數(shù)

2025-02-27 00:00:30

SpringJava方式

2021-08-05 07:28:25

Java實(shí)現(xiàn)方式

2009-02-11 09:46:00

ASON網(wǎng)絡(luò)演進(jìn)

2024-05-10 07:44:23

C#進(jìn)程程序

2021-05-07 16:19:36

異步編程Java線程

2022-03-23 12:55:50

農(nóng)業(yè)物聯(lián)網(wǎng)

2020-07-31 11:12:39

安全威脅網(wǎng)絡(luò)攻擊網(wǎng)絡(luò)安全

2020-04-27 09:00:00

雙因素認(rèn)證身份認(rèn)證生物識(shí)別

2022-01-14 10:34:50

黑客隱藏蹤跡網(wǎng)絡(luò)安全
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)