執行緒控制時間的隨筆(例項)

碼農不可能碼農的發表於2015-04-15
 1 package kk;
 2 import java.util.*;
 3 import java.awt.EventQueue;
 4 import java.text.DecimalFormat;
 5 import java.text.SimpleDateFormat;
 6 import java.util.Scanner;
 7 import java.util.Timer;
 8 import java.util.TimerTask;
 9 
10 import javax.swing.JOptionPane;
11 
12 
13 
14 
15 public class kk {
16     float sum;
17     String parten = "0.00";//為下一行程式碼初始化準備
18     DecimalFormat decimal = new DecimalFormat(parten);//建立一個型別為DecimalFormat類的物件decimal
19     String str;
20     static Timer timer = null; 
21     static SimpleDateFormat df=new SimpleDateFormat("mm:ss"); //控制時間的格式類   
22     long starttime;
23     long finishtime;
24     long duration;
25     long allduration;
26     public kk()
27     {
28          timer = new Timer(); 
29          finishtime=System.currentTimeMillis();
30          starttime=finishtime;
31          timer.schedule(new TimerTask() 
32          {
33              public void run() {    
34                  finishtime=System.currentTimeMillis();
35                  duration=finishtime-starttime;
36                 /* if(duration/1000==25&&summary<tishu)
37                  {
38                      JOptionPane.showMessageDialog(null, "答題時間已到。答題總題數為"+(dadui+dacuo)+",答對題數為"+dadui+",答錯題數為"+dacuo+",未答題題數為"+(tishu-dadui-dacuo)+",答題使用時間:"+(allduration+duration)/1000+"秒。");
39                      frame.setVisible(false);
40                      new 每天一練初級版題型選擇();
41                  }
42                  String str=df.format(new Date(duration)); 
43                  T7.setText(str);*/
44                  String str=df.format(new Date(duration)); 
45                  System.out.print(str);
46                  }  
47              }, 1000, 1000);
48     }
49     public static void main(String[] args) {
50         EventQueue.invokeLater(new Runnable() {
51             public void run() {
52                 try {
53                     kk window = new kk();
54                     Scanner qq=new Scanner(System.in);                 
55                 } catch (Exception e) {
56                     e.printStackTrace();
57                 }
58             }
59         });
60     }
61 }
執行緒控制時間

 

相關文章