本文主要是介绍多线程环境下SimpleDateFormat类安全转换,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
一、SimpleDateFormat类
package state;import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;/*** SimpleDateFormat类负责日期的转换与格式化* 解决SimpleDateFormat类多线程环境下转换错误问题* @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;}@Overridepublic void run() {try {//System.out.println(dateString);//刚开始日期格式设置错误为YYYY-MM-ddDate 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+" 转换日期"+newDateString);}else {System.out.println("ThreadName="+this.getName()+"日期"+dateString+" 转换日期"+newDateString);}} catch (ParseException e) {// TODO Auto-generated catch blocke.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;}
}
二、线程状态和线程组
(1)线程5状态
//NEW 尚未启动的线程
//RUNNABLE Java虚拟机中执行的线程
//TERMINATED 已经退出的线程
//BLOCKED 阻塞受监视的线程
//WAITING 无限等待另一个线程
//TIMED_WAITING 等待另一个线程指定时间
package state;
/*** 线程中状态和线程组* @author zc**/
public class MyThread extends Thread{//NEW//RUNNABLE//TERMINATED//BLOCKED//WAITING//TIMED_WAITINGpublic MyThread() {System.out.println("构造方法中的状态: "+Thread.currentThread().getState());}@Overridepublic void run() {System.out.println("run方法中的状态:"+Thread.currentThread().getState());try {Thread.sleep(1000);} catch (InterruptedException e) {// TODO Auto-generated catch blocke.printStackTrace();}}public static void main(String[] args) {MyThread myThread = new MyThread();System.out.println("main方法中的状态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("活动的线程数量为: " +group.activeCount());System.out.println("线程组的名称为: "+group.getName());try {myThread.start();Thread.sleep(1000);System.out.println("main方法中的状态2 "+ myThread.getState());} catch (InterruptedException e) {// TODO Auto-generated catch blocke.printStackTrace();}}
}
这篇关于多线程环境下SimpleDateFormat类安全转换的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!