Java线程同步 获取系统时间_JAVA_编程开发_程序员俱乐部

中国优秀的程序员网站程序员频道CXYCLUB技术地图
热搜:
更多>>
 
您所在的位置: 程序员俱乐部 > 编程开发 > JAVA > Java线程同步 获取系统时间

Java线程同步 获取系统时间

 2012/6/11 0:11:50  liulijun.cn.2011  程序员俱乐部  我要评论(0)
  • 摘要:需求:Web系统中有多处地方会将当前系统中的信息采用多种方式保存到同一文件夹中,当多人同时要保存同一份信息的场合,经常会出现文件名重复。导致一系列问题。文件名称的区分是以某些前缀+yyyy-MM-ddHH:mm:ss组成的。所以区分的关键就锁定到日期部分。使日期部分不重复。importjava.util.Calendar;importorg.apache.commons.lang3.time.DateFormatUtils;publicclassCalendarUtils
  • 标签:Java 线程
需求:Web系统中有多处地方会将当前系统中的信息采用多种方式保存到同一文件夹中,
当多人同时要保存同一份信息的场合,经常会出现文件名重复。导致一系列问题。文件名称的区分是以    某些前缀 + yyyy-MM-dd HH:mm:ss  组成的。所以区分的关键就锁定到日期部分。使日期部分不重复。

import java.util.Calendar;

import org.apache.commons.lang3.time.DateFormatUtils;

public class CalendarUtils {
	
	public String getCurrendTime() {
		synchronized (CalendarUtils.class) {
			try {
				Thread.sleep(1000);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
			
			Calendar calendar = Calendar.getInstance();
			
			return DateFormatUtils.format(calendar, "yyyy-MM-dd HH:mm:ss");		
		}
	}
}


首先使用synchronized关键字保证线程同步,并且同步对象要为CalendarUtils。
第二使用线程睡眠,保证每条线程访问的间隔为1秒钟。
输出结果:
Thread4==2012-06-10 18:07:45
Thread3==2012-06-10 18:07:46
Thread6==2012-06-10 18:07:47
Thread2==2012-06-10 18:07:48
Thread5==2012-06-10 18:07:49
Thread1==2012-06-10 18:07:50
Thread5==2012-06-10 18:07:51
Thread2==2012-06-10 18:07:52
Thread6==2012-06-10 18:07:53
Thread3==2012-06-10 18:07:54
Thread4==2012-06-10 18:07:55
Thread3==2012-06-10 18:07:56
Thread6==2012-06-10 18:07:57
Thread2==2012-06-10 18:07:58
Thread5==2012-06-10 18:07:59
Thread1==2012-06-10 18:08:00
Thread5==2012-06-10 18:08:01
Thread2==2012-06-10 18:08:02
Thread6==2012-06-10 18:08:03
Thread3==2012-06-10 18:08:04
Thread4==2012-06-10 18:08:05
Thread3==2012-06-10 18:08:06
Thread6==2012-06-10 18:08:07
Thread2==2012-06-10 18:08:08
Thread5==2012-06-10 18:08:09
Thread1==2012-06-10 18:08:10
Thread5==2012-06-10 18:08:11
Thread2==2012-06-10 18:08:12
Thread6==2012-06-10 18:08:13
Thread3==2012-06-10 18:08:14
Thread4==2012-06-10 18:08:15
Thread3==2012-06-10 18:08:16
Thread6==2012-06-10 18:08:17
Thread2==2012-06-10 18:08:18
Thread5==2012-06-10 18:08:19
Thread1==2012-06-10 18:08:20
Thread5==2012-06-10 18:08:21
Thread2==2012-06-10 18:08:22
Thread6==2012-06-10 18:08:23
Thread3==2012-06-10 18:08:24
Thread4==2012-06-10 18:08:25
Thread3==2012-06-10 18:08:26
Thread6==2012-06-10 18:08:27
Thread2==2012-06-10 18:08:28
Thread5==2012-06-10 18:08:29
Thread1==2012-06-10 18:08:30
Thread5==2012-06-10 18:08:31
Thread2==2012-06-10 18:08:32
Thread6==2012-06-10 18:08:33
Thread3==2012-06-10 18:08:34
Thread4==2012-06-10 18:08:35
Thread3==2012-06-10 18:08:36
Thread6==2012-06-10 18:08:37
Thread2==2012-06-10 18:08:38
Thread5==2012-06-10 18:08:39
Thread1==2012-06-10 18:08:40
Thread5==2012-06-10 18:08:41
Thread2==2012-06-10 18:08:42
Thread6==2012-06-10 18:08:43
Thread3==2012-06-10 18:08:44
Thread4==2012-06-10 18:08:45
Thread3==2012-06-10 18:08:46
Thread6==2012-06-10 18:08:47
Thread2==2012-06-10 18:08:48
Thread5==2012-06-10 18:08:49
Thread1==2012-06-10 18:08:50
Thread5==2012-06-10 18:08:51
Thread2==2012-06-10 18:08:52
Thread6==2012-06-10 18:08:53
Thread3==2012-06-10 18:08:54
Thread4==2012-06-10 18:08:55
Thread3==2012-06-10 18:08:56
Thread6==2012-06-10 18:08:57
Thread2==2012-06-10 18:08:58
Thread5==2012-06-10 18:08:59
Thread1==2012-06-10 18:09:00
Thread5==2012-06-10 18:09:01
Thread2==2012-06-10 18:09:02
Thread6==2012-06-10 18:09:03
Thread3==2012-06-10 18:09:04
Thread4==2012-06-10 18:09:05
Thread3==2012-06-10 18:09:06
Thread6==2012-06-10 18:09:07
Thread2==2012-06-10 18:09:08
Thread5==2012-06-10 18:09:09
Thread1==2012-06-10 18:09:10
Thread5==2012-06-10 18:09:11
Thread2==2012-06-10 18:09:12
Thread6==2012-06-10 18:09:13
Thread3==2012-06-10 18:09:14
Thread4==2012-06-10 18:09:15
Thread3==2012-06-10 18:09:16
Thread6==2012-06-10 18:09:17
Thread2==2012-06-10 18:09:18
Thread5==2012-06-10 18:09:19
Thread1==2012-06-10 18:09:20
Thread5==2012-06-10 18:09:21
Thread2==2012-06-10 18:09:22
Thread6==2012-06-10 18:09:23
Thread3==2012-06-10 18:09:24
Thread4==2012-06-10 18:09:25

发表评论
用户名: 匿名