java中的接口是类吗
265
2022-12-15
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
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-http://generated catch block
e.printStackTrace();
}
}
public static void main(String[] args) {
MyThhttp://read 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();
}
}
}
版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。
发表评论
暂时没有评论,来抢沙发吧~