日出星辰

          線程學習筆記【2】---Timer(定時器)

          入門

          public class Time01 {

          public static void main(String[] args) {

          // Timer timer01=new Timer();
          // timer01.schedule(new TimerTask(){
          //
          // @Override
          // public void run() {
          //
          // System.out.println("bombing");
          // }}, 1000);

          new Timer().schedule(new TimerTask() {

          @Override
          public void run() {
          System.out.println(
          "bombing");

          }
          },
          10000);
          while (true) {
          System.out.println(
          new Date().getSeconds());
          try {
          Thread.sleep(
          1000);
          }
          catch (InterruptedException e) {
          e.printStackTrace();
          }
          }
          }

          }

          運行結果

          21
          22
          23
          24
          25
          26
          27
          28
          29
          30
          bombing
          31
          32
          33
          連續執行

          public class Time02 {

          public static void main(String[] args) {

          new Timer().schedule(new TimerTask() {

          @Override
          public void run() {
          System.out.println(
          "bombing");

          }
          },
          10000,3000); //每隔3秒執行
          while (true) {
          System.out.println(
          new Date().getSeconds());
          try {
          Thread.sleep(
          1000);
          }
          catch (InterruptedException e) {
          e.printStackTrace();
          }
          }
          }

          }

          運行結果:

          50
          51
          52
          53
          54
          55
          56
          57
          58
          59
          bombing
          0
          1
          2
          bombing
          3
          4
          5
          bombing
          6
          7
          提高

           匿名內部類是臨時的

           整數分為奇數和偶數,所以可以按照奇偶操作完成

          /**
          *一個2秒執行,另一個4秒執行,交替循環往復
          *
          */
          public class Time03 {

          static int i = 0; // 靜態變量可以記錄類創建的對象數量

          public static void main(String[] args) {
          class MyTimerTask extends TimerTask {
          //內部類內部不能生成靜態變量
          public void run() {
          i
          =(i+1)%2;
          System.out.println(
          "bombing");
          new Timer().schedule(new MyTimerTask(), 2000+2000*i);
          }
          }
          new Timer().schedule(new MyTimerTask(), 2000);
          while (true) {
          try {
          System.out.println(
          new Date().getSeconds());
          Thread.sleep(
          1000);
          }
          catch (InterruptedException e) {
          e.printStackTrace();
          }
          }

          }

          }

           運行結果:

          13
          14
          bombing
          15
          16
          17
          18
          bombing
          19
          20
          bombing
          21
          22
          23
          24
          bombing
          25

          /**
          * 每天在指定的時間執行操作
          * 提供調度需求的開源框架Quartz在這方面處理能力很強
          */
          public class Timer04 {

          public static void main(String[] args) {
          String str
          = "2011-08-28 08:39:00";
          SimpleDateFormat sdf
          = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

          Timer timer
          = new Timer();
          try {
          timer.schedule(
          new TimerTask() {

          @Override
          public void run() {

          System.out.println(
          "timer");
          }
          }, sdf.parse(str),
          24 * 60 * 1000);
          }
          catch (ParseException e) {
          e.printStackTrace();
          }

          while (true) {
          System.out.println(
          new Date().getSeconds());
          try {
          Thread.sleep(
          1000);
          }
          catch (InterruptedException e) {
          e.printStackTrace();
          }
          }
          }
          /**
          * 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
          * 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 timer 0 1 2 3 4 5
          */

          }

           

          posted on 2011-08-28 09:15 日出星辰 閱讀(71) 評論(0)  編輯  收藏


          只有注冊用戶登錄后才能發表評論。


          網站導航:
           
          主站蜘蛛池模板: 庆元县| 铜梁县| 东乡县| 泰州市| 商水县| 高州市| 景洪市| 余干县| 江永县| 威远县| 青海省| 嵊泗县| 仪征市| 禹城市| 宕昌县| 静安区| 漯河市| 台山市| 青岛市| 五台县| 崇礼县| 永宁县| 陆良县| 屏南县| 弋阳县| 宽甸| 荔波县| 阳新县| 桦川县| 宣威市| 宁国市| 沐川县| 包头市| 塘沽区| 宜章县| 个旧市| 盐山县| 偏关县| 许昌市| 安仁县| 湛江市|