public class Main {
public static void main(String[] args) {
new JmsStartThread().start();
}
}
public class JmsStartThread extends Thread{
public void run(){
TimeTask.getInstance().startTimeTask();
}
}
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
public class TimeTask {
//定时周期执行指定的任务
private static final ScheduledExecutorService SCHEDULED
= Executors.newScheduledThreadPool(1);
//单例模式*************
private static final class TimeTaskEg{
private static final TimeTask timeTask = new TimeTask();
}
private TimeTask(){}
public static TimeTask getInstance(){
return TimeTaskEg.timeTask;
}
/**
* 启动定时任务查询端口是否联通
*/
public void startTimeTask(){
SCHEDULED.scheduleAtFixedRate(PortThread.getInstance(), 0, 30, TimeUnit.SECONDS);
//方法:scheduleAtFixedRate(Runnable command,执行线程
long initialDelay,初始化延时
long period,两次开始最小间隔时间
Timeunit unit计时单位
当执行任务的时间大于我们指定的间隔时间时,他并不会在指定间隔时开辟一个新的线程并发执行这个任务。而是等待该线程执行完毕。
}
public void stopTimeTask(){
SCHEDULED.shutdownNow();
}
}
import java.io.IOException;
import java.net.Socket;
import java.net.UnknownHostException;
public class PortThread implements Runnable{
private static final class PortThreadEg{
private static final PortThread portThread = new PortThread();
}
private PortThread(){}
public static PortThread getInstance(){
return PortThreadEg.portThread;
}
@Override
public void run() {
try {
Socket socket = new Socket("10.0.0.2",443);
socket.close();
System.out.println("port connect");
} catch (UnknownHostException e) {
System.out.println("port not connect");
} catch (IOException e) {
System.out.println("port not connect");
}
}
}
Timer执行周期任务时依赖系统时间,如果当前系统时间发生变化会出现一些执行上的变化。
ScheduledExecuterService基于时间的延迟,不会由于系统时间的改变发生执行变化
public static void main(String[] args) {
new JmsStartThread().start();
}
}
public class JmsStartThread extends Thread{
public void run(){
TimeTask.getInstance().startTimeTask();
}
}
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
public class TimeTask {
//定时周期执行指定的任务
private static final ScheduledExecutorService SCHEDULED
= Executors.newScheduledThreadPool(1);
//单例模式*************
private static final class TimeTaskEg{
private static final TimeTask timeTask = new TimeTask();
}
private TimeTask(){}
public static TimeTask getInstance(){
return TimeTaskEg.timeTask;
}
/**
* 启动定时任务查询端口是否联通
*/
public void startTimeTask(){
SCHEDULED.scheduleAtFixedRate(PortThread.getInstance(), 0, 30, TimeUnit.SECONDS);
//方法:scheduleAtFixedRate(Runnable command,执行线程
long initialDelay,初始化延时
long period,两次开始最小间隔时间
Timeunit unit计时单位
当执行任务的时间大于我们指定的间隔时间时,他并不会在指定间隔时开辟一个新的线程并发执行这个任务。而是等待该线程执行完毕。
}
public void stopTimeTask(){
SCHEDULED.shutdownNow();
}
}
import java.io.IOException;
import java.net.Socket;
import java.net.UnknownHostException;
public class PortThread implements Runnable{
private static final class PortThreadEg{
private static final PortThread portThread = new PortThread();
}
private PortThread(){}
public static PortThread getInstance(){
return PortThreadEg.portThread;
}
@Override
public void run() {
try {
Socket socket = new Socket("10.0.0.2",443);
socket.close();
System.out.println("port connect");
} catch (UnknownHostException e) {
System.out.println("port not connect");
} catch (IOException e) {
System.out.println("port not connect");
}
}
}
Timer执行周期任务时依赖系统时间,如果当前系统时间发生变化会出现一些执行上的变化。
ScheduledExecuterService基于时间的延迟,不会由于系统时间的改变发生执行变化