多线程环境下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 t = new 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;
	}
}

二、线程状态和线程组

(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_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学习专栏,java编程,java多线程,java,多线程,并发编程,thread)