您的位置:首页 > 编程语言 > Java开发

Java开发可能用到的代码片段

2012-09-04 00:00 393 查看
1.check if the input paramter is number:

if(!(args[1].trim().matches("[\\d.]+"))){

System.out.println("The input is not number.");

System.exit(-1);

}

2.将propertie文件读入内存

FileInputStream inputFile = new FileInputStream("filepath");

Properties propertie = new Properties();

propertie.load(inputFile);

inputFile.close();

3.java.util.Timer

与每个 Timer 对象相对应的是单个后台线程,用于顺序地执行所有计时器任务。计时器任务应该迅速完成。如果完成某个计时器任务的时间太长,那么它会“独占”计时器的任务执行线程。因此,这就可能延迟后续任务的执行,而这些任务就可能“堆在一起”,并且在上述不友好的任务最终完成时才能够被快速连续地执行

A facility for threads to schedule tasks for future execution in a background thread.

Tasks may be scheduled for one-time execution, or for repeated execution at regular intervals.

private void crontab(int delayTime,int spaceTime)

{

Timer timer = new Timer(true);

timer.schedule(new TimerTask(){

public void run(){

someTask();

}

},delayTime,spaceTime);

}

比较新的java API实现:

java.util.concurrent.ScheduledExecutorService.scheduleWithFixedDelay(Runnable command,long initialDelay,long delay,TimeUnit unit)
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  java 常用代码