import org.junit.Test;
import redis.clients.jedis.Jedis;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Timer;
import java.util.TimerTask;
/**
* Created by xiaohuihui on 2018-07-09
*/
public class RedisDemo {
public static void main(String[] args) throws ParseException {
//日期格式工具
final SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
final Jedis jedis = new Jedis("localhost");
jedis.setex("timeout", 10, "hello");
Timer timer = new Timer();
//12秒后执行定时器,每1s执行一次
System.out.print(sdf.format(new Date()));
System.out.println("the timer two will be executed after 12 seconds...");
//启动后延迟时间
long afterSs = 9999;
//执行周期
long intervalSs1 = 1 * 1000;
timer.schedule(new TimerTask() {
// 执行计数器
int i = 0;
@Override
public void run() {
System.out.print(sdf.format(new Date()));
System.out.println("the timer two has execution " + (++i) + " timers");
String timeout = jedis.get("timeout");
jedis.setex("timeout", 10, "hello" + i);
System.out.println(timeout);
// 执行10次后关闭定时器
/* if (i == 10) {
this.cancel();
}*/
}
}, afterSs, intervalSs1);
}
@Test
public void testJedis() {
Jedis jedis = new Jedis("localhost");
jedis.set("company", "yuntai");
String company = jedis.get("company");
System.out.println(company);
}
@Test
public void testRedisTimeOut() throws ParseException {
//日期格式工具
final SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Timer timer = new Timer();
// 10s后执行定时器,仅执行一次
System.out.print(sdf.format(new Date()));
System.out.println("the timer one will be executed after 10 seconds...");
long milliseconds = 10 * 1000;
timer.schedule(new TimerTask() {
@Override
public void run() {
System.out.print(sdf.format(new Date()));
System.out.println("the timer one has finished execution");
}
}, milliseconds);
//12秒后执行定时器,每1s执行一次
System.out.print(sdf.format(new Date()));
System.out.println("the timer two will be executed after 12 seconds...");
//启动后延迟时间
long afterSs = 12 * 1000;
//执行周期
long intervalSs1 = 1 * 1000;
timer.schedule(new TimerTask() {
// 执行计数器
int i = 0;
@Override
public void run() {
System.out.print(sdf.format(new Date()));
System.out.println("the timer two has execution " + (++i) + " timers");
// 执行10次后关闭定时器
if (i == 10) {
this.cancel();
}
}
}, afterSs, intervalSs1);
// 指定时间执行定时器,仅执行一次
System.out.print(sdf.format(new Date()));
System.out.println("the timer three will be executed at 2017-06-27 21:47:00...");
Date date = sdf.parse("2018-07-09 22:16:00");
timer.schedule(new TimerTask() {
@Override
public void run() {
System.out.print(sdf.format(new Date()));
System.out.println("the timer three has finished execution");
}
}, date);
// 从指定时间开始周期性执行
System.out.print(sdf.format(new Date()));
System.out.println("the timer four will be executed at 2017-06-27 21:48:00...");
// 执行间隔周期
long intervalSs = 1 * 1000;
// 开始执行时间
Date beginTime = sdf.parse("2018-07-09 22:17:00");
timer.schedule(new TimerTask() {
// 执行计数器
int i = 0;
@Override
public void run() {
System.out.print(sdf.format(new Date()));
System.out.println("the timer four has execution " + (++i) + " timers");
// 执行10次后关闭定时器
if (i == 10) {
this.cancel();
}
}
}, beginTime, intervalSs);
}
@Test
public void testTimerTask() {
//创建定时器
// Timer timer=new Timer();
//final变量是用来保证对象的安全发布,防止对象引用被其他线程在对象被完全构造完成前拿到并使用。
final Timer timer = new Timer("线程名称");
//设置定时任务和时间
/*1.安排在指定延迟后执行指定的任务*/
timer.schedule(new TimerTask() {
@Override
public void run() {
}
}, 100);//100毫秒
/*2.设置在指定的时间执行任务*/
Date date = new Date();
timer.schedule(new TimerTask() {
@Override
public void run() {
}
}, date);
/*3.安排指定的任务在指定的延迟后开始进行重复的固定速率执行。*/
timer.scheduleAtFixedRate(new TimerTask() {
@Override
public void run() {
}
}, 100, 30 * 1000);//延迟100,周期30秒
/*4.安排指定的任务在指定的时间开始进行重复的固定速率执行。*/
Date date2 = new Date();
timer.scheduleAtFixedRate(new TimerTask() {
@Override
public void run() {
}
}, date, 30 * 1000);//周期30秒
/*计时结束,停止所有的计时任务*/
long time = System.currentTimeMillis();
Date t_date = new Date(time);
timer.schedule(new TimerTask() {
@Override
public void run() {
timer.cancel();//计时结束,停止所有的计时任务
}
}, t_date);
}
}
jedis及Redis过期
最新推荐文章于 2023-03-18 07:36:36 发布