DelayQueue的简单介绍

DelayQueue按照延迟时间从小到大出队列的队列,延迟时间表示的是未来将要执行的时间减去当前的时间,对于加入DelayQueue的元素,需要实现Delayed接口

 

 当getDelay()的返回值小于或者等于0时,说明该元素要到期了,需要从队列中拿出来了

 

 

 当延迟时间大于0,队列处于阻塞状态,直到延迟时间满足条件为止

 代码部分

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
 
    <groupId>com.java</groupId>
    <artifactId>test-study</artifactId>
    <version>1.0-SNAPSHOT</version>
 
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.2.1.RELEASE</version>
        <relativePath/>
    </parent>
 
    <dependencies>
        <!--tomcat容器-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <!--lombok依赖-->
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.18.16</version>
        </dependency>
        <!--引入junit单元测试依赖-->
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
        </dependency>
        <!--判断空的用法  -->
        <dependency>
            <groupId>commons-lang</groupId>
            <artifactId>commons-lang</artifactId>
            <version>2.6</version>
        </dependency>
 
        <!-- https://mvnrepository.com/artifact/com.oracle.database.jdbc/ojdbc8 -->
        <dependency>
            <groupId>com.oracle.database.jdbc</groupId>
            <artifactId>ojdbc8</artifactId>
            <version>12.2.0.1</version>
        </dependency>
        <!--springboot整合mybatis-->
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>2.1.2</version>
        </dependency>
 
        <!--添加fastjson依赖-->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
            <version>1.2.70</version>
        </dependency>
        <!-- 热部署模块 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <optional>true</optional> <!-- 这个需要为 true 热部署才有效 -->
        </dependency>
        <!--ThreadFactoryBuilder的依赖包,多线程使用-->
        <dependency>
            <groupId>com.google.guava</groupId>
            <artifactId>guava</artifactId>
            <version>30.1-jre</version>
        </dependency>
 
        <!--Lists.partition要用的依赖-->
        <dependency>
            <groupId>com.google.guava</groupId>
            <artifactId>guava</artifactId>
            <version>21.0</version>
        </dependency>
       <!--ListUtils.partition使用的依赖-->
        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-collections4</artifactId>
            <version>4.4</version>
        </dependency>
 
 
    </dependencies>
 
 
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
        <finalName>study</finalName>
    </build>
 
 
</project>

  

1
2
3
4
5
6
server.port=2001
 
 
logging.level.com.java.test=debug
logging.level.web=debug
spring.devtools.add-properties=false

  

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
package com.java.test.config;
 
import com.google.common.util.concurrent.ThreadFactoryBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;
 
import java.util.concurrent.*;
 
/**
 * @Description:
 * @Author: Yourheart
 * @Create: 2022/11/25 14:43
 */
@Component
public class ThreadPoolDemo {
 
    @Bean
    public ExecutorService createThreadPool(){
        /**
         * 线程资源必须通过线程池提供,不允许在应用中自行显式创建线程。
         * 说明:使用线程池的好处是减少在创建和销毁线程上所花的时间以及系统资源的开销,
         * 解决资源不足的问题。如果不使用线程池,有可能造成系统创建大量同类线程而导致消耗完内存或者“过度切换”的问题。
         */
 
        ThreadFactory namedThreadFactory = new ThreadFactoryBuilder()
                .setNameFormat("qiuxie-pool-%d").build();
        ExecutorService singleThreadPool = new ThreadPoolExecutor(1, 2,
                1000L, TimeUnit.MILLISECONDS,
                new LinkedBlockingQueue<>(1024), namedThreadFactory, new ThreadPoolExecutor.AbortPolicy());
        return  singleThreadPool;
    }
}

  

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
package com.java.test.delays.queues;
 
import java.util.concurrent.Delayed;
import java.util.concurrent.TimeUnit;
 
/**
 * @Description:
 * @Author: Yourheart
 * @Create: 2022/11/29 14:39
 */
public class MyDelayedTask implements Delayed {
 
    private String name ;
    private long start = System.currentTimeMillis();
    private long time ;
 
    public MyDelayedTask(String name,long time) {
        this.name = name;
        this.time = time;
    }
 
    @Override
    public long getDelay(TimeUnit unit) {
        return unit.convert((start+time) - System.currentTimeMillis(),TimeUnit.MILLISECONDS);
    }
 
    @Override
    public int compareTo(Delayed o) {
        MyDelayedTask o1 = (MyDelayedTask) o;
        return (int) (this.getDelay(TimeUnit.MILLISECONDS) - o.getDelay(TimeUnit.MILLISECONDS));
    }
 
    @Override
    public String toString() {
        return "MyDelayedTask{" +
                "name='" + name + '\'' +
                ", start=" + start +
                ", time=" + time +
                '}';
    }
}

  

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
package com.java.test.delays.queues;
 
import lombok.extern.slf4j.Slf4j;
 
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.Delayed;
 
/**
 * @Description
 * @Author qiuxie
 * @Date 2022/11/30 0:14
 */
@Slf4j
public class DelayQueueOfferThread implements  Runnable{
 
 
    private BlockingQueue<Delayed> delayQueue;
 
    public DelayQueueOfferThread(BlockingQueue<Delayed> delayQueue) {
        this.delayQueue = delayQueue;
    }
 
    @Override
    public void run() {
        log.info("【延迟队列中放置数据...】");
        delayQueue.offer(new MyDelayedTask("task1", 10000));
        delayQueue.offer(new MyDelayedTask("task2", 3900));
        delayQueue.offer(new MyDelayedTask("task3", 1900));
        delayQueue.offer(new MyDelayedTask("task4", 5900));
        delayQueue.offer(new MyDelayedTask("task5", 6900));
        delayQueue.offer(new MyDelayedTask("task6", 7900));
        delayQueue.offer(new MyDelayedTask("task7", 4900));
        delayQueue.offer(new MyDelayedTask(null,2300));
 
    }
}

  

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
package com.java.test.delays.queues;
 
import com.java.test.config.ThreadPoolDemo;
import lombok.extern.slf4j.Slf4j;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
 
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.DelayQueue;
import java.util.concurrent.Delayed;
 
/**
 * @author yourheart
 * @Description
 * @create 2022-11-17 22:55
 */
@Slf4j
@Configuration      //1.主要用于标记配置类,兼备Component的效果。
@EnableScheduling   // 2.开启定时任务
public class DelayQueueDemo {
 
    @Autowired
    private ThreadPoolDemo threadPoolDemo;
 
    BlockingQueue<Delayed> delayQueue = new DelayQueue<>();
 
    @Scheduled(cron = "0 23 0 * * *"//凌晨12点21分执行
    @Async
    public void test() throws InterruptedException {
 
        threadPoolDemo.createThreadPool().execute(new DelayQueueOfferThread(delayQueue));
 
    }
 
    @Scheduled(cron = "0 25 0 * * *")
    @Async
    public void takeData() throws InterruptedException {
        while (true) {
            String name = Thread.currentThread().getName();
            Thread.State state = Thread.currentThread().getState();
            log.info("name:{},state:{}",name,state);
            Delayed take = delayQueue.take();
            log.info("take:{}",take);
        }
    }
}

  

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
package com.java.test;
 
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
 
/**
 * @Description:
 * @Author: Yourheart
 * @Create: 2022/10/20 15:32
 */
@SpringBootApplication
public class TestApplication {
    public static void main(String[] args) {
        SpringApplication.run(TestApplication.class,args);
    }
}

  

 

 

 

 

 

 

 

 和其他的区别在于,队列为空会阻塞,延迟时间未到也会阻塞

 

posted @   不忘初心2021  阅读(37)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· AI 智能体引爆开源社区「GitHub 热点速览」
· C#/.NET/.NET Core技术前沿周刊 | 第 29 期(2025年3.1-3.9)
· 从HTTP原因短语缺失研究HTTP/2和HTTP/3的设计差异
历史上的今天:
2021-11-30 idea整合mybatis实现简单分页
点击右上角即可分享
微信分享提示