本文目錄一覽:
- 1、Java怎麼給方法計時?
- 2、做一個Java計時器?
- 3、用JAVA編寫計時器
- 4、如何用java實現一個計時器?
- 5、java中如何實現自動計時功能,就是點擊一個start按鈕就開始計時,以秒為單位
Java怎麼給方法計時?
你可以在開始和結束的時候,分別記錄下當前的時間的這毫秒數。然後再減,以下是一段代碼。
public class Test{
public static void main(String[] args) {
long startMili=System.currentTimeMillis();// 當前時間對應的毫秒數
System.out.println(“開始 “+startMili);
// 執行一段代碼,求一百萬次隨機值
for(int i=0;i1000000;i++){
Math.random();
}
long endMili=System.currentTimeMillis();
System.out.println(“結束 s”+endMili);
System.out.println(“總耗時為:”+(endMili-startMili)+”毫秒”);
}
}
做一個Java計時器?
您好,茫茫人海之中,能為君排憂解難實屬朕的榮幸,在下拙見,若有錯誤,還望見諒!。展開全部
怎麼還沒人回答,看不過去了,用不用多線程根據你的程序需要,
import java.io.IOException;
import java.util.Timer;
public class TimerTest {
public static void main(String[] args){
Timer timer = new Timer();
timer.schedule(new MyTask(), 1000, 2000);//在1秒後執行此任務,每次間隔2秒,如果傳遞一個Data參數,就可以在某個固定的時間執行這個任務.
while(true){//這個是用來停止此任務的,否則就一直循環執行此任務了
try {
int ch = System.in.read();
if(ch-‘c’==0){
timer.cancel();//使用這個方法退出任務
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
static class MyTask extends java.util.TimerTask{
@Override
public void run() {
//你要進行的操作
}
}
}
大概就是這樣了,在根據你的業務需要查一下資料,就可以搞定了!非常感謝您的耐心觀看,如有幫助請採納,祝生活愉快!謝謝!
用JAVA編寫計時器
計時器可以使用timer類也可以使用線程類來操作,下面是Thread做的簡單的計時器
public class Calculagraph extends Thread {
public static void main(String[] args) {
new Calculagraph().start();
}
private long now = 0l;
private long start = System.currentTimeMillis();// 程序啟動時間的毫秒值
private long time;
public void run() {
while (true) {
now = System.currentTimeMillis();// 獲取一秒之後的毫秒值
time = now – start;// 兩個時間相減的到毫秒差
System.out.format(“%02d:%02d:%02d\n”,
time / (1000 * 60 * 60) % 60/* 時 */,
time / (1000 * 60)% 60/* 分 */,
time / 1000 % 60/* 秒 */);// 格式化字元串輸出
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
如何用java實現一個計時器?
用java實現一個計時器的方法:
public class TestDingShi implements Runnable
{
Thread xc;
Dao dao=new DaoImpl();
public TestDingShi()
{
xc=new Thread(this);//線程開啟
xc.start();
}
public void run()
{
while (true)
{
try
{
xc.sleep(1000);//睡眠開始計時
}
catch (InterruptedException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
//TODO定時在此
}
}
}
java中如何實現自動計時功能,就是點擊一個start按鈕就開始計時,以秒為單位
簡單代碼如下:
import java.awt.Button;
import java.awt.FlowLayout;
import java.awt.Label;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.swing.JFrame;
import javax.swing.Timer;
@SuppressWarnings(“serial”)
public class Timers extends JFrame {
final Label lab = new Label();
Date now = new Date();
@SuppressWarnings(“deprecation”)
public Timers() {
now.setHours(0);
now.setMinutes(0);
now.setSeconds(0);
setBounds(550, 270, 200, 150);
final Timer timer = new Timer(1000, new ActionListener() {
public void actionPerformed(ActionEvent e) {
Date now2 = new Date(now.getTime() + 1000);
now = now2;
SimpleDateFormat formatter = new SimpleDateFormat(“HH:mm:ss”);
lab.setText(formatter.format(now));
}
});
Button b1 = new Button(“開始”);
Button b2 = new Button(“停止”);
b2.setBounds(40, 40, 40, 40);
b1.setBounds(30, 30, 30, 30);
b1.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
Button b = (Button) e.getSource();
b.setLabel(“開始”);
timer.start();
}
});
b2.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
Button b = (Button) e.getSource();
b.setLabel(“停止”);
timer.stop();
}
});
this.setLayout(new FlowLayout());
this.add(b2);
this.add(b1);
this.add(lab);
this.setSize(300, 200);
this.setVisible(true);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
}
public static void main(String[] args) {
Timers t = new Timers();
t.lab.setText(“00:00:00”);
}
}
不知是否幫到你,如滿意請採納!
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-tw/n/310128.html