亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

Java的DelayQueue延遲隊(duì)列簡(jiǎn)單使用代碼實(shí)例

 更新時(shí)間:2023年12月13日 10:32:39   作者:Terisadeng  
這篇文章主要介紹了Java的DelayQueue延遲隊(duì)列簡(jiǎn)單使用代碼實(shí)例,DelayQueue是一個(gè)延遲隊(duì)列,插入隊(duì)列的數(shù)據(jù)只有達(dá)到設(shè)置的延遲時(shí)間時(shí)才能被取出,否則線程會(huì)被阻塞,插入隊(duì)列的對(duì)象必須實(shí)現(xiàn)Delayed接口,需要的朋友可以參考下

DelayQueue延遲隊(duì)列代碼實(shí)例

DelayQueue是一個(gè)延遲隊(duì)列,插入隊(duì)列的數(shù)據(jù)只有達(dá)到設(shè)置的延遲時(shí)間時(shí)才能被取出,否則線程會(huì)被阻塞。

插入隊(duì)列的對(duì)象必須實(shí)現(xiàn)Delayed接口,實(shí)現(xiàn)comapreTo方法和getDelay方法,其中g(shù)etDelay方法用于設(shè)置對(duì)象延遲多少秒取出,compareTo用于對(duì)被延遲取出的數(shù)據(jù)進(jìn)行排序

所以compareTo方法中一般調(diào)用對(duì)象的getDelay方法,根據(jù)對(duì)象的延遲時(shí)間進(jìn)行排序。

首先是定義實(shí)現(xiàn)Delayed接口的類(lèi):

import java.util.concurrent.Delayed;
import java.util.concurrent.TimeUnit;
/**
 * 實(shí)現(xiàn)Delayed接口,用于插入延遲阻塞隊(duì)列
 * @author SN
 *
 */
public class Worker implements Delayed{
	private String name;
	private String workNo;
	private long startTime;
	private long endTime;
	public static final TimeUnit TIME_UNIT=TimeUnit.SECONDS;
	public Worker(String name,String workNo,long startTime,long endTime){
		this.name=name;
		this.workNo=workNo;
		this.startTime=startTime;
		this.endTime=endTime;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getWorkNo() {
		return workNo;
	}
	public void setWorkNo(String workNo) {
		this.workNo = workNo;
	}
	public long getStartTime() {
		return startTime;
	}
	public void setStartTime(long startTime) {
		this.startTime = startTime;
	}
	public long getEndTime() {
		return endTime;
	}
	public void setEndTime(long endTime) {
		this.endTime = endTime;
	}
	//排序方法,用于將插入隊(duì)列的對(duì)象按延遲時(shí)間進(jìn)行排序
	@Override
	public int compareTo(Delayed delayed) {
		Worker worker=(Worker) delayed;
		return (this.getDelay(TIME_UNIT)-worker.getDelay(TIME_UNIT))>0?1:0;
	}
	//延遲時(shí)間,用于確定取出隊(duì)列中數(shù)據(jù)的時(shí)間
	@Override
	public long getDelay(TimeUnit unit) {
		return endTime-System.currentTimeMillis();
	}
}

然后是測(cè)試向DelayQueue插入數(shù)據(jù)和取出數(shù)據(jù):

import java.sql.Date;
import java.text.SimpleDateFormat;
import java.util.concurrent.DelayQueue;
/**
 * 向延遲隊(duì)列中插入數(shù)據(jù),并在到達(dá)延遲時(shí)間時(shí)從隊(duì)列中取出數(shù)據(jù),否則就阻塞當(dāng)前線程
 * @author SN
 *
 */
public class DelayQueueExp implements Runnable{
	private DelayQueue<Worker> queue=new DelayQueue<>();
	public boolean working=true;
	public static final SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
	//向延遲隊(duì)列中插入數(shù)據(jù),并設(shè)置延遲取出的時(shí)間
	public void startWork(String name,String workNo,int grade){
		long time=System.currentTimeMillis();
		Date date=new Date(time);
		Worker worker=new Worker(name, workNo, System.currentTimeMillis(), grade*10000+time);
		System.out.println("員工"+worker.getName()+",工號(hào)"+worker.getWorkNo()+"于"+sdf.format(date)+"打卡上班...");
		this.queue.add(worker);
	}
	public void endWork(Worker worker){
		Date date=new Date(worker.getEndTime());
		System.out.println("員工"+worker.getName()+",工號(hào)"+worker.getWorkNo()+"于"+sdf.format(date)+"打卡下班...");
	}
	@Override
	public void run() {
		while (working) {
			try {
				//啟動(dòng)線程后,從延遲隊(duì)列中取出數(shù)據(jù),如果沒(méi)有滿(mǎn)足延遲時(shí)間條件的數(shù)據(jù)可以取出就阻塞住線程
				Worker worker=queue.take();
				endWork(worker);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
	}
	public static void main(String[] args) {
		System.out.println("員工開(kāi)始打卡上班...");
		DelayQueueExp company=new DelayQueueExp();
		Thread work=new Thread(company);
		work.start();
		//向延遲隊(duì)列中插入數(shù)據(jù)
		company.startWork("zhangsan", "1001", 1);
		company.startWork("lisi", "1002", 3);
		company.startWork("wanger", "1003", 5);
	}
}

到此這篇關(guān)于Java的DelayQueue延遲隊(duì)列簡(jiǎn)單使用代碼實(shí)例的文章就介紹到這了,更多相關(guān)DelayQueue延遲隊(duì)列代碼實(shí)例內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • springboot結(jié)合maven配置不同環(huán)境的profile方式

    springboot結(jié)合maven配置不同環(huán)境的profile方式

    這篇文章主要介紹了springboot結(jié)合maven配置不同環(huán)境的profile方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-01-01
  • SpringCloud Config統(tǒng)一配置中心問(wèn)題分析解決與客戶(hù)端動(dòng)態(tài)刷新實(shí)現(xiàn)

    SpringCloud Config統(tǒng)一配置中心問(wèn)題分析解決與客戶(hù)端動(dòng)態(tài)刷新實(shí)現(xiàn)

    springcloud config是一個(gè)解決分布式系統(tǒng)的配置管理方案。它包含了 client和server兩個(gè)部分,server端提供配置文件的存儲(chǔ)、以接口的形式將配置文件的內(nèi)容提供出去,client端通過(guò)接口獲取數(shù)據(jù)、并依據(jù)此數(shù)據(jù)初始化自己的應(yīng)用
    2022-10-10
  • Java中使用LocalDate根據(jù)日期來(lái)計(jì)算年齡的實(shí)現(xiàn)方法

    Java中使用LocalDate根據(jù)日期來(lái)計(jì)算年齡的實(shí)現(xiàn)方法

    這篇文章主要介紹了Java中使用LocalDate根據(jù)日期來(lái)計(jì)算年齡的實(shí)現(xiàn)方法,需要的朋友可以參考下
    2018-01-01
  • Java?Object類(lèi)和包裝類(lèi)深入解讀

    Java?Object類(lèi)和包裝類(lèi)深入解讀

    Object類(lèi)是一個(gè)特殊的類(lèi),是所有類(lèi)的父類(lèi),如果一個(gè)類(lèi)沒(méi)有用extends明確指出繼承于某個(gè)類(lèi),那么它默認(rèn)繼承Object類(lèi),所謂包裝類(lèi),就是能夠直接將簡(jiǎn)單類(lèi)型的變量表示為一個(gè)類(lèi),在執(zhí)行變量類(lèi)型的相互轉(zhuǎn)換時(shí),我們會(huì)大量使用這些包裝類(lèi)
    2022-02-02
  • eclipse 如何創(chuàng)建 user library 方法詳解

    eclipse 如何創(chuàng)建 user library 方法詳解

    這篇文章主要介紹了eclipse 如何創(chuàng)建 user library 方法詳解的相關(guān)資料,需要的朋友可以參考下
    2017-04-04
  • Spring @RequestMapping 注解及使用技巧詳解

    Spring @RequestMapping 注解及使用技巧詳解

    @RequestMapping是Spring MVC 中定義請(qǐng)求映射規(guī)則的核心注解,用于將HTTP請(qǐng)求映射到Controller處理方法,下面給大家介紹Spring @RequestMapping 注解及使用技巧,感興趣的朋友一起看看吧
    2025-06-06
  • Java算法中的歸并排序算法代碼實(shí)現(xiàn)

    Java算法中的歸并排序算法代碼實(shí)現(xiàn)

    這篇文章主要介紹了Java算法中的歸并排序算法代碼實(shí)現(xiàn),歸并排序使用的是分治思想(Divide and Conquer),分治,顧名思義,就是分而治之,是將一個(gè)大問(wèn)題分解成小的子問(wèn)題來(lái)解決,需要的朋友可以參考下
    2023-12-12
  • 使用@RequiredArgsConstructor注解來(lái)取代繁瑣的@Autowrired

    使用@RequiredArgsConstructor注解來(lái)取代繁瑣的@Autowrired

    有了@RequiredArgsConstructor注解,我們就可以減少@Autowired的書(shū)寫(xiě),本文主要介紹了使用@RequiredArgsConstructor注解來(lái)取代繁瑣的@Autowrired,感興趣的可以了解一下
    2022-04-04
  • Java使用try-with-resources實(shí)現(xiàn)自動(dòng)解鎖

    Java使用try-with-resources實(shí)現(xiàn)自動(dòng)解鎖

    項(xiàng)目中使用Redission分布式鎖,每次使用都需要顯示的解鎖,很麻煩,Java 提供了 try-with-resources 語(yǔ)法糖,它不僅可以用于自動(dòng)關(guān)閉流資源,還可以用于實(shí)現(xiàn)自動(dòng)解鎖,本文將介紹如何利用 try-with-resources 實(shí)現(xiàn)鎖的自動(dòng)釋放,需要的朋友可以參考下
    2025-01-01
  • Java在PowerPoint中添加上標(biāo)和下標(biāo)的實(shí)現(xiàn)方法

    Java在PowerPoint中添加上標(biāo)和下標(biāo)的實(shí)現(xiàn)方法

    當(dāng)我們?cè)谘菔疚母逯刑砑由虡?biāo)、版權(quán)或其他符號(hào)時(shí),我們可能希望該符號(hào)出現(xiàn)在某個(gè)文本的上方或下方。在Microsoft PowerPoint中,我們可以通過(guò)對(duì)符號(hào)應(yīng)用上標(biāo)或下標(biāo)格式來(lái)實(shí)現(xiàn)這種效果,這篇文章主要介紹了Java在PowerPoint中添加上標(biāo)和下標(biāo),需要的朋友可以參考下
    2022-10-10

最新評(píng)論