您好,登錄后才能下訂單哦!
一、SimpleDateFormat類
package state; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Date; /** * SimpleDateFormat類負(fù)責(zé)日期的轉(zhuǎn)換與格式化 * 解決SimpleDateFormat類多線程環(huán)境下轉(zhuǎn)換錯誤問題 * @author zc * */ public class SimpleDateFormatThread extends Thread{ private SimpleDateFormat sdf; private String dateString; public SimpleDateFormatThread(SimpleDateFormat sdf,String dateString) { super(); this.sdf =sdf; this.dateString =dateString; } @Override public void run() { try { //System.out.println(dateString); //剛開始日期格式設(shè)置錯誤為YYYY-MM-dd Date date =DateTools.getSimpleDateFormat("yyyy-MM-dd").parse(dateString); // System.out.println(date); String newDateString =DateTools.getSimpleDateFormat("yyyy-MM-dd").format(date).toString(); if(!newDateString.equals(dateString)) { System.out.println("ThreadName="+this.getName()+"報錯日期"+dateString+" 轉(zhuǎn)換日期"+newDateString); }else { System.out.println("ThreadName="+this.getName()+"日期"+dateString+" 轉(zhuǎn)換日期"+newDateString); } } catch (ParseException e) { // TODO Auto-generated catch block e.printStackTrace(); } } public static void main(String[] args) { SimpleDateFormat sdf =new SimpleDateFormat("yyyy-MM-dd"); String[] dateStringArray =new String[]{ "2020-02-05","2020-02-06","2020-02-07","2020-02-08" }; SimpleDateFormatThread []threadArray= new SimpleDateFormatThread[4]; for(int i=0;i<4;i++) { threadArray[i] =new SimpleDateFormatThread(sdf,dateStringArray[i]); } for(int i=0;i<4;i++) { threadArray[i].start(); } } } class DateTools{ //ThreadLocal能使線程綁定到指定的對象 private static ThreadLocal<SimpleDateFormat> t = new ThreadLocal<SimpleDateFormat>(); public static SimpleDateFormat getSimpleDateFormat(String datePattern) { SimpleDateFormat sdf=null; sdf =t.get(); if(sdf==null) { sdf =new SimpleDateFormat(datePattern); t.set(sdf); } return sdf; } }
二、線程狀態(tài)和線程組
(2)線程5狀態(tài) //NEW 尚未啟動的線程 //RUNNABLE Java虛擬機中執(zhí)行的線程 //TERMINATED 已經(jīng)退出的線程 //BLOCKED 阻塞受監(jiān)視的線程 //WAITING 無限等待另一個線程 //TIMED_WAITING 等待另一個線程指定時間 package state; /** * 線程中狀態(tài)和線程組 * @author zc * */ public class MyThread extends Thread{ //NEW //RUNNABLE //TERMINATED //BLOCKED //WAITING //TIMED_WAITING public MyThread() { System.out.println("構(gòu)造方法中的狀態(tài): "+Thread.currentThread().getState()); } @Override public void run() { System.out.println("run方法中的狀態(tài):"+Thread.currentThread().getState()); try { Thread.sleep(1000); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } public static void main(String[] args) { MyThread myThread = new MyThread(); System.out.println("main方法中的狀態(tài)1 "+ myThread.getState()); Thread t1 = new Thread(); Thread t2 = new Thread(); ThreadGroup group =new ThreadGroup("左氏線程組"); Thread t3 = new Thread(group,t1); Thread t4 = new Thread(group,t2); t3.start(); t4.start(); System.out.println("活動的線程數(shù)量為: " +group.activeCount()); System.out.println("線程組的名稱為: "+group.getName()); try { myThread.start(); Thread.sleep(1000); System.out.println("main方法中的狀態(tài)2 "+ myThread.getState()); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持億速云。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。