java延遲執(zhí)行代碼 java程序如何延遲兩秒

在JAVA中怎樣能使一條語句延遲執(zhí)行

可以試試java.lang.Thread里的sleep方法

公司主營業(yè)務(wù):成都做網(wǎng)站、成都網(wǎng)站建設(shè)、移動網(wǎng)站開發(fā)等業(yè)務(wù)。幫助企業(yè)客戶真正實現(xiàn)互聯(lián)網(wǎng)宣傳,提高企業(yè)的競爭能力。成都創(chuàng)新互聯(lián)是一支青春激揚、勤奮敬業(yè)、活力青春激揚、勤奮敬業(yè)、活力澎湃、和諧高效的團隊。公司秉承以“開放、自由、嚴謹、自律”為核心的企業(yè)文化,感謝他們對我們的高要求,感謝他們從不同領(lǐng)域給我們帶來的挑戰(zhàn),讓我們激情的團隊有機會用頭腦與智慧不斷的給客戶帶來驚喜。成都創(chuàng)新互聯(lián)推出興縣免費做網(wǎng)站回饋大家。

public static void sleep(long millis)

throws InterruptedException在指定的毫秒數(shù)內(nèi)讓當前正在執(zhí)行的線程休眠(暫停執(zhí)行),此操作受到系統(tǒng)計時器和調(diào)度程序精度和準確性的影響。該線程不丟失任何監(jiān)視器的所屬權(quán)。 參數(shù):

millis - 以毫秒為單位的休眠時間。

拋出:

InterruptedException - 如果任何線程中斷了當前線程。當拋出該異常時,當前線程的中斷狀態(tài) 被清除。

java中如何是方法延遲執(zhí)行?

顯示JDialog之前啟動一個線程,等待3秒后使JDialog消失

注意,一定要在JDialog.setVisible(true)之前啟動線程,否則,一旦啟動JDialog,就會阻塞住程序,使后面的代碼無法執(zhí)行

java executors有哪些方法

Java通過Executors提供四種線程池,分別為:

newCachedThreadPool創(chuàng)建一個可緩存線程池,如果線程池長度超過處理需要,可靈活回收空閑線程,若無可回收,則新建線程。

newFixedThreadPool 創(chuàng)建一個定長線程池,可控制線程最大并發(fā)數(shù),超出的線程會在隊列中等待。

newScheduledThreadPool 創(chuàng)建一個定長線程池,支持定時及周期性任務(wù)執(zhí)行。

newSingleThreadExecutor 創(chuàng)建一個單線程化的線程池,它只會用唯一的工作線程來執(zhí)行任務(wù),保證所有任務(wù)按照指定順序(FIFO, LIFO, 優(yōu)先級)執(zhí)行。

(1) newCachedThreadPool

創(chuàng)建一個可緩存線程池,如果線程池長度超過處理需要,可靈活回收空閑線程,若無可回收,則新建線程。示例代碼如下:

Java代碼

package?test;

import?java.util.concurrent.ExecutorService;

import?java.util.concurrent.Executors;

public?class?ThreadPoolExecutorTest?{

public?static?void?main(String[]?args)?{

ExecutorService?cachedThreadPool?=?Executors.newCachedThreadPool();

for?(int?i?=?0;?i??10;?i++)?{

final?int?index?=?i;

try?{

Thread.sleep(index?*?1000);

}?catch?(InterruptedException?e)?{

e.printStackTrace();

}

cachedThreadPool.execute(new?Runnable()?{

public?void?run()?{

System.out.println(index);

}

});

}

}

}

線程池為無限大,當執(zhí)行第二個任務(wù)時第一個任務(wù)已經(jīng)完成,會復用執(zhí)行第一個任務(wù)的線程,而不用每次新建線程。

(2) newFixedThreadPool(項目用過)

創(chuàng)建一個定長線程池,可控制線程最大并發(fā)數(shù),超出的線程會在隊列中等待。示例代碼如下:

Java代碼

package?test;

import?java.util.concurrent.ExecutorService;

import?java.util.concurrent.Executors;

public?class?ThreadPoolExecutorTest?{

public?static?void?main(String[]?args)?{

ExecutorService?fixedThreadPool?=?Executors.newFixedThreadPool(3);

for?(int?i?=?0;?i??10;?i++)?{

final?int?index?=?i;

fixedThreadPool.execute(new?Runnable()?{

public?void?run()?{

try?{

System.out.println(index);

Thread.sleep(2000);

}?catch?(InterruptedException?e)?{

e.printStackTrace();

}

}

});

}

}

}

因為線程池大小為3,每個任務(wù)輸出index后sleep 2秒,所以每兩秒打印3個數(shù)字。

定長線程池的大小最好根據(jù)系統(tǒng)資源進行設(shè)置。如Runtime.getRuntime().availableProcessors()

(3)? newScheduledThreadPool

創(chuàng)建一個定長線程池,支持定時及周期性任務(wù)執(zhí)行。延遲執(zhí)行示例代碼如下:

Java代碼

package?test;

import?java.util.concurrent.Executors;

import?java.util.concurrent.ScheduledExecutorService;

import?java.util.concurrent.TimeUnit;

public?class?ThreadPoolExecutorTest?{

public?static?void?main(String[]?args)?{

ScheduledExecutorService?scheduledThreadPool?=?Executors.newScheduledThreadPool(5);

scheduledThreadPool.schedule(new?Runnable()?{

public?void?run()?{

System.out.println("delay?3?seconds");

}

},?3,?TimeUnit.SECONDS);

}

}

表示延遲3秒執(zhí)行。

定期執(zhí)行示例代碼如下:

Java代碼

package?test;

import?java.util.concurrent.Executors;

import?java.util.concurrent.ScheduledExecutorService;

import?java.util.concurrent.TimeUnit;

public?class?ThreadPoolExecutorTest?{

public?static?void?main(String[]?args)?{

ScheduledExecutorService?scheduledThreadPool?=?Executors.newScheduledThreadPool(5);

scheduledThreadPool.scheduleAtFixedRate(new?Runnable()?{

public?void?run()?{

System.out.println("delay?1?seconds,?and?excute?every?3?seconds");

}

},?1,?3,?TimeUnit.SECONDS);

}

}

表示延遲1秒后每3秒執(zhí)行一次。

(4) newSingleThreadExecutor

創(chuàng)建一個單線程化的線程池,它只會用唯一的工作線程來執(zhí)行任務(wù),保證所有任務(wù)按照指定順序(FIFO, LIFO, 優(yōu)先級)執(zhí)行。示例代碼如下:

Java代碼

package?test;

import?java.util.concurrent.ExecutorService;

import?java.util.concurrent.Executors;

public?class?ThreadPoolExecutorTest?{

public?static?void?main(String[]?args)?{

ExecutorService?singleThreadExecutor?=?Executors.newSingleThreadExecutor();

for?(int?i?=?0;?i??10;?i++)?{

final?int?index?=?i;

singleThreadExecutor.execute(new?Runnable()?{

public?void?run()?{

try?{

System.out.println(index);

Thread.sleep(2000);

}?catch?(InterruptedException?e)?{

e.printStackTrace();

}

}

});

}

}

}

結(jié)果依次輸出,相當于順序執(zhí)行各個任務(wù)。

你可以使用JDK自帶的監(jiān)控工具來監(jiān)控我們創(chuàng)建的線程數(shù)量,運行一個不終止的線程,創(chuàng)建指定量的線程,來觀察:

工具目錄:C:\Program Files\Java\jdk1.6.0_06\bin\jconsole.exe

運行程序做稍微修改:

Java代碼

package?test;

import?java.util.concurrent.ExecutorService;

import?java.util.concurrent.Executors;

public?class?ThreadPoolExecutorTest?{

public?static?void?main(String[]?args)?{

ExecutorService?singleThreadExecutor?=?Executors.newCachedThreadPool();

for?(int?i?=?0;?i??100;?i++)?{

final?int?index?=?i;

singleThreadExecutor.execute(new?Runnable()?{

public?void?run()?{

try?{

while(true)?{

System.out.println(index);

Thread.sleep(10?*?1000);

}

}?catch?(InterruptedException?e)?{

e.printStackTrace();

}

}

});

try?{

Thread.sleep(500);

}?catch?(InterruptedException?e)?{

e.printStackTrace();

}

}

}

}

網(wǎng)頁題目:java延遲執(zhí)行代碼 java程序如何延遲兩秒
轉(zhuǎn)載注明:http://muchs.cn/article10/ddipjdo.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站設(shè)計、Google、品牌網(wǎng)站制作、網(wǎng)站維護品牌網(wǎng)站建設(shè)、網(wǎng)站排名

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

商城網(wǎng)站建設(shè)