您好,登錄后才能下訂單哦!
這篇文章主要介紹了java SimpleDateFormat怎么轉(zhuǎn)換局部變量的相關(guān)知識(shí),內(nèi)容詳細(xì)易懂,操作簡(jiǎn)單快捷,具有一定借鑒價(jià)值,相信大家閱讀完這篇java SimpleDateFormat怎么轉(zhuǎn)換局部變量文章都會(huì)有所收獲,下面我們一起來(lái)看看吧。
說(shuō)明
1、將SimpleDateFormat定義為局部變量時(shí),每個(gè)線程都獨(dú)占SimpleDateFormat目標(biāo)。
2、相當(dāng)于將多線程序改為單線程序程序,因此不存在線程不安全的問(wèn)題。
實(shí)例
import java.text.SimpleDateFormat; import java.util.Date; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; public class SimpleDateFormatExample { public static void main(String[] args) { // 創(chuàng)建線程池 ExecutorService threadPool = Executors.newFixedThreadPool(10); // 執(zhí)行 10 次時(shí)間格式化 for (int i = 0; i < 10; i++) { int finalI = i; // 線程池執(zhí)行任務(wù) threadPool.execute(new Runnable() { @Override public void run() { // 創(chuàng)建 SimpleDateFormat 對(duì)象 SimpleDateFormat simpleDateFormat = new SimpleDateFormat("mm:ss"); // 創(chuàng)建時(shí)間對(duì)象 Date date = new Date(finalI * 1000); // 執(zhí)行時(shí)間格式化并打印結(jié)果 System.out.println(simpleDateFormat.format(date)); } }); } // 任務(wù)執(zhí)行完之后關(guān)閉線程池 threadPool.shutdown(); } }
關(guān)于“java SimpleDateFormat怎么轉(zhuǎn)換局部變量”這篇文章的內(nèi)容就介紹到這里,感謝各位的閱讀!相信大家對(duì)“java SimpleDateFormat怎么轉(zhuǎn)換局部變量”知識(shí)都有一定的了解,大家如果還想學(xué)習(xí)更多知識(shí),歡迎關(guān)注億速云行業(yè)資訊頻道。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。