java中常见的3种自动任务
2016-03-25 16:58
423 查看
一、线程循环
package com.test.timetask;
public class ThreadLine {
@SuppressWarnings("deprecation")
public static void gorun(int i) {
final long timeInterval = 5 * 1000;
Runnable runnable = new Runnable() {
@Override
public void run() {
int j = 1;
while (true) {
System.out.println("某某第" + j++ + "次循环!");
try {
Thread.sleep(timeInterval);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
};
Thread thread = new Thread(runnable);
if (i > 0) {
thread.stop();
} else {
thread.start();
}
}
public static void test() {
while (true) {
System.out.println("nihao");
try {
Thread.sleep(7000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public static void main(String[] args) {
test();
}
}
二、TimerTask
package com.test.timetask;
import java.util.Timer;
import java.util.TimerTask;
public class TimerTask1 extends TimerTask {
@Override
public void run() {
// TODO Auto-generated method stub
System.out.println("cencen");
}
public static void main(String[] args) {
TimerTask1 task1 = new TimerTask1();
Timer timer = new Timer(false);
long delay = 0;
long intevalPeriod = 3 * 1000;
// schedules the task to be run in an interval
timer.schedule(task1, delay, intevalPeriod);
// timer.cancel();
}
// end of main
}
三、scheduleAtFixedRate
package com.test.timetask;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
public class TimerTask3{
public static void main(String[] args) {
Runnable runnable = new Runnable() {
public void run() {
// task to run goes here
System.out.println("Hello !!");
}
};
ScheduledExecutorService service = Executors.newSingleThreadScheduledExecutor();
//任务,启动多久开始执行,执行的时间间隔,时间单位
service.scheduleAtFixedRate(runnable, 6, 5, TimeUnit.SECONDS);
}
}
package com.test.timetask;
public class ThreadLine {
@SuppressWarnings("deprecation")
public static void gorun(int i) {
final long timeInterval = 5 * 1000;
Runnable runnable = new Runnable() {
@Override
public void run() {
int j = 1;
while (true) {
System.out.println("某某第" + j++ + "次循环!");
try {
Thread.sleep(timeInterval);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
};
Thread thread = new Thread(runnable);
if (i > 0) {
thread.stop();
} else {
thread.start();
}
}
public static void test() {
while (true) {
System.out.println("nihao");
try {
Thread.sleep(7000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public static void main(String[] args) {
test();
}
}
二、TimerTask
package com.test.timetask;
import java.util.Timer;
import java.util.TimerTask;
public class TimerTask1 extends TimerTask {
@Override
public void run() {
// TODO Auto-generated method stub
System.out.println("cencen");
}
public static void main(String[] args) {
TimerTask1 task1 = new TimerTask1();
Timer timer = new Timer(false);
long delay = 0;
long intevalPeriod = 3 * 1000;
// schedules the task to be run in an interval
timer.schedule(task1, delay, intevalPeriod);
// timer.cancel();
}
// end of main
}
三、scheduleAtFixedRate
package com.test.timetask;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
public class TimerTask3{
public static void main(String[] args) {
Runnable runnable = new Runnable() {
public void run() {
// task to run goes here
System.out.println("Hello !!");
}
};
ScheduledExecutorService service = Executors.newSingleThreadScheduledExecutor();
//任务,启动多久开始执行,执行的时间间隔,时间单位
service.scheduleAtFixedRate(runnable, 6, 5, TimeUnit.SECONDS);
}
}
相关文章推荐
- Java8初体验 Stream语法详解
- java实现七个医生值班问题
- 深入理解Java:注解(Annotation)--注解处理器
- Java Socket编程
- wsdl 关于nillable和minOccurs 在.NET和java中的不同
- java中一对多 关联建表
- Eclipse中jsp、js文件编辑时,卡死现象解决汇总
- Eclipse 中使用 ctrl 无法追踪函数的问题
- 详细介绍如何在Eclipse中使用SVN
- Java 加解密技术系列之 PBE RSA AES DH 3DES DES HMAC SHA MD5 BASE64
- Java并行编程–从并行任务集获取反馈
- 基于spring-jdbc的BaseDao(包含分页)
- Java对象克隆(Clone)及Cloneable接口、Serializable接口的深入探讨
- Java集合中Comparable与Comparator
- 18、java中json怎么运用?
- java基础--HashSet详解
- java常见的排序算法
- Java new一个对象执行几个过程
- java基础--去除List中重复的元素
- 注解实现struts2零配置