本篇内容主要讲解“Java多线程环境下SimpleDateFormat类的安全转换方法”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“Java多线程环境下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; } @Override public void run() { try { //System.out.println(dateString); //刚开始日期格式设置错误为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+" 转换日期"+newDateString); }else { System.out.println("ThreadName="+this.getName()+"日期"+dateString+" 转换日期"+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; }}
二、线程状态和线程组
(2)线程5状态//NEW 尚未启动的线程//RUNNABLE Java虚拟机中执行的线程//TERMINATED 已经退出的线程//BLOCKED 阻塞受监视的线程//WAITING 无限等待另一个线程//TIMED_WAITING 等待另一个线程指定时间package state;/** * 线程中状态和线程组 * @author zc * */public class MyThread extends Thread{ //NEW //RUNNABLE //TERMINATED //BLOCKED //WAITING //TIMED_WAITING public MyThread() { System.out.println("构造方法中的状态: "+Thread.currentThread().getState()); } @Override public void run() { System.out.println("run方法中的状态:"+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方法中的状态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 block e.printStackTrace(); } }}
到此,相信大家对“Java多线程环境下SimpleDateFormat类的安全转换方法”有了更深的了解,不妨来实际操作一番吧!这里是亿速云网站,更多相关内容可以进入相关频道进行查询,关注我们,继续学习!
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。