Skip to content

Commit 4a56973

Browse files
committed
新增: Timer的使用
1 parent 54023d6 commit 4a56973

File tree

1 file changed

+44
-0
lines changed

1 file changed

+44
-0
lines changed
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package org.javacore.scheduler; /*
2+
* Copyright [2015] [Jeff Lee]
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
import java.util.Timer;
18+
import java.util.TimerTask;
19+
20+
/**
21+
* Timer的使用
22+
* @author BYSocket
23+
* @since 2016-01-09 22:19:00
24+
*/
25+
public class TimerTest extends TimerTask{
26+
27+
private final String jobName;
28+
29+
public TimerTest(String jobName) {
30+
this.jobName = jobName;
31+
}
32+
33+
@Override
34+
public void run() {
35+
System.out.println("run the task => " + jobName);
36+
}
37+
38+
public static void main(String[] args) {
39+
// 一种工具,线程用其安排以后在后台线程中执行的任务
40+
Timer timer = new Timer();
41+
timer.schedule(new TimerTest("Job 1") , 1000 , 1000); // 一秒
42+
timer.schedule(new TimerTest("Job 2") , 2000 , 2000); // 两秒
43+
}
44+
}

0 commit comments

Comments
 (0)