File tree Expand file tree Collapse file tree 6 files changed +238
-0
lines changed
src/main/java/com/chen/api/util/thread/study/chapter5/timerTest1 Expand file tree Collapse file tree 6 files changed +238
-0
lines changed Original file line number Diff line number Diff line change 1+ package com .chen .api .util .thread .study .chapter5 .timerTest1 ;
2+
3+ import java .util .Timer ;
4+ import java .util .TimerTask ;
5+
6+ /**
7+ * @author Chen WeiJie
8+ * @date 2018-05-30 20:42:55
9+ **/
10+ public class Run1 {
11+
12+ //定时器,创建一个Timer就是启动一个新的线程。
13+ private static Timer timer = new Timer ();
14+
15+ //任务块
16+ static public class MyTask extends TimerTask {
17+ @ Override
18+ public void run () {
19+ System .out .println ("执行了:" + System .currentTimeMillis ());
20+ }
21+ }
22+
23+ public static void main (String [] args ) {
24+ MyTask task = new MyTask ();
25+ System .out .println ("开始了:" + System .currentTimeMillis ());
26+ //在10秒后执行
27+ timer .schedule (task , 10000 );
28+ }
29+
30+
31+ }
Original file line number Diff line number Diff line change 1+ package com .chen .api .util .thread .study .chapter5 .timerTest1 ;
2+
3+ import java .util .Timer ;
4+ import java .util .TimerTask ;
5+
6+ /**
7+ * @author Chen WeiJie
8+ * @date 2018-05-30 20:42:55
9+ **/
10+ public class Run2 {
11+
12+ //定时器,创建一个Timer就是启动一个新的线程。
13+ private static Timer timer = new Timer ();
14+
15+ //任务块
16+ static public class MyTask extends TimerTask {
17+ @ Override
18+ public void run () {
19+ System .out .println ("threadName:" + Thread .currentThread ().getName () + ",执行了:" + System .currentTimeMillis ()/1000 );
20+ }
21+ }
22+
23+ public static void main (String [] args ) {
24+ MyTask task1 = new MyTask ();
25+ MyTask task2 = new MyTask ();
26+ MyTask task3 = new MyTask ();
27+ MyTask task4 = new MyTask ();
28+ MyTask task5 = new MyTask ();
29+ //TimerTask是以队列的方式顺序执行的,所以执行的时间有可能和预期的时间不一致,因为前面的任务有可能消耗的时间较长,则后面的任务会被顺延。
30+ timer .schedule (task1 ,1000 );
31+ timer .schedule (task2 ,9000 );
32+ timer .schedule (task3 ,5000 );
33+ timer .schedule (task4 ,6000 );
34+ timer .schedule (task5 ,8000 );
35+ System .out .println ("开始了:" + System .currentTimeMillis ()/1000 );
36+ //在10秒后执行
37+ }
38+
39+
40+ }
Original file line number Diff line number Diff line change 1+ package com .chen .api .util .thread .study .chapter5 .timerTest1 ;
2+
3+ import java .text .ParseException ;
4+ import java .text .SimpleDateFormat ;
5+ import java .util .Timer ;
6+ import java .util .TimerTask ;
7+
8+ /**
9+ * 某一时刻开始,每隔N秒执行一次,无限循环的执行某一任务
10+ *
11+ * @author Chen WeiJie
12+ * @date 2018-05-30 21:05:00
13+ **/
14+ public class Run3 {
15+
16+
17+ private static Timer timer = new Timer ();
18+
19+ static public class MyTask extends TimerTask {
20+ @ Override
21+ public void run () {
22+ System .out .println ("执行了任务:" + System .currentTimeMillis () / 1000 );
23+ }
24+ }
25+
26+ public static void main (String [] args ) {
27+
28+ MyTask task = new MyTask ();
29+ SimpleDateFormat sdf = new SimpleDateFormat ("yyyy-MM-dd HH:mm:ss" );
30+
31+ String dateStr = "2018-05-30 21:11:30" ;
32+ try {
33+ timer .schedule (task , sdf .parse (dateStr ), 4000 );
34+ } catch (ParseException e ) {
35+ e .printStackTrace ();
36+ }
37+
38+
39+ }
40+
41+
42+ }
Original file line number Diff line number Diff line change 1+ package com .chen .api .util .thread .study .chapter5 .timerTest1 ;
2+
3+ import java .text .ParseException ;
4+ import java .text .SimpleDateFormat ;
5+ import java .util .Timer ;
6+ import java .util .TimerTask ;
7+
8+ /**
9+ * @author Chen WeiJie
10+ * @date 2018-05-30 21:16:18
11+ **/
12+ public class Run4 {
13+
14+
15+ static public class MyTaskA extends TimerTask {
16+ @ Override
17+ public void run () {
18+ System .out .println ("a任务开始执行。。" );
19+ //timerTask的方法,将task从定时任务中移除出去
20+ this .cancel ();
21+ }
22+ }
23+
24+ static public class MyTaskB extends TimerTask {
25+ @ Override
26+ public void run () {
27+ System .out .println ("b任务开始执行。。" );
28+ }
29+ }
30+
31+
32+ public static void main (String [] args ) {
33+
34+ try {
35+ SimpleDateFormat sdf = new SimpleDateFormat ("yyyy-MM-dd HH:mm:ss" );
36+ String dateStr = "2018-05-30 21:21:50" ;
37+
38+ MyTaskA taskA = new MyTaskA ();
39+ MyTaskB taskB = new MyTaskB ();
40+ Timer timer = new Timer ();
41+
42+ timer .schedule (taskA , sdf .parse (dateStr ),1000 );
43+ timer .schedule (taskB , sdf .parse (dateStr ),2000 );
44+
45+ System .out .println ("开始执行。。。。。" );
46+ } catch (ParseException e ) {
47+ e .printStackTrace ();
48+ }
49+
50+
51+ }
52+
53+
54+ }
Original file line number Diff line number Diff line change 1+ package com .chen .api .util .thread .study .chapter5 .timerTest1 ;
2+
3+ import java .text .ParseException ;
4+ import java .text .SimpleDateFormat ;
5+ import java .util .Timer ;
6+ import java .util .TimerTask ;
7+
8+ /**
9+ * @author Chen WeiJie
10+ * @date 2018-05-30 21:16:18
11+ **/
12+ public class Run5 {
13+
14+
15+ static public class MyTaskA extends TimerTask {
16+ @ Override
17+ public void run () {
18+ System .out .println ("a任务开始执行。。" );
19+ }
20+ }
21+
22+ static public class MyTaskB extends TimerTask {
23+ @ Override
24+ public void run () {
25+ System .out .println ("b任务开始执行。。" );
26+ }
27+ }
28+
29+
30+ public static void main (String [] args ) {
31+
32+ try {
33+ SimpleDateFormat sdf = new SimpleDateFormat ("yyyy-MM-dd HH:mm:ss" );
34+ String dateStr = "2018-05-30 21:22:50" ;
35+
36+ MyTaskA taskA = new MyTaskA ();
37+ MyTaskB taskB = new MyTaskB ();
38+ Timer timer = new Timer ();
39+
40+ timer .schedule (taskA , sdf .parse (dateStr ), 1000 );
41+ timer .schedule (taskB , sdf .parse (dateStr ), 1000 );
42+ System .out .println ("开始执行。。。。。" );
43+ Thread .sleep (10000 );
44+ //timer的cancel类,timer定时器类取消
45+ timer .cancel ();
46+ } catch (ParseException | InterruptedException e ) {
47+ e .printStackTrace ();
48+ }
49+
50+
51+ }
52+
53+
54+ }
Original file line number Diff line number Diff line change 1+ package com .chen .api .util .thread .study .chapter5 .timerTest1 ;
2+
3+ /**
4+ * scheduleAtFixedRate(TimerTask task,Date firstTime,long period)
5+ * schedule 和scheduleAtFixedRate主要区别在于不延时的情况,
6+ * schedule如果执行的任务没有被延时,那么下一次任务的执行时间参考的执行时间是上一次任务“开始”的时间来计算。
7+ * scheduleAtFixedRate 如果执行的任务没有被延时,那么下一次任务的执行时间参考的执行时间是上一次任务“结束”的时间来计算。
8+ *
9+ * 延时的情况则没有区别,都是按照结束的时间来计算
10+ *
11+ * @author Chen WeiJie
12+ * @date 2018-05-30 21:27:37
13+ **/
14+ public class Run6 {
15+
16+ }
17+
You can’t perform that action at this time.
0 commit comments