用java實(shí)現(xiàn)一個(gè)計(jì)時(shí)器的方法:
10余年的封丘網(wǎng)站建設(shè)經(jīng)驗(yàn),針對(duì)設(shè)計(jì)、前端、開發(fā)、售后、文案、推廣等六對(duì)一服務(wù),響應(yīng)快,48小時(shí)及時(shí)工作處理。成都全網(wǎng)營銷推廣的優(yōu)勢是能夠根據(jù)用戶設(shè)備顯示端的尺寸不同,自動(dòng)調(diào)整封丘建站的顯示方式,使網(wǎng)站能夠適用不同顯示終端,在瀏覽器中調(diào)整網(wǎng)站的寬度,無論在任何一種瀏覽器上瀏覽網(wǎng)站,都能展現(xiàn)優(yōu)雅布局與設(shè)計(jì),從而大程度地提升瀏覽體驗(yàn)。成都創(chuàng)新互聯(lián)公司從事“封丘網(wǎng)站設(shè)計(jì)”,“封丘網(wǎng)站推廣”以來,每個(gè)客戶項(xiàng)目都認(rèn)真落實(shí)執(zhí)行。
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);//睡眠開始計(jì)時(shí)
}
catch (InterruptedException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
//TODO定時(shí)在此
}
}
}
計(jì)時(shí)器可以使用timer類也可以使用線程類來操作,下面是Thread做的簡單的計(jì)時(shí)器
public?class?Calculagraph?extends?Thread?{
public?static?void?main(String[]?args)?{
new?Calculagraph().start();
}
private?long?now?=?0l;
private?long?start?=?System.currentTimeMillis();//?程序啟動(dòng)時(shí)間的毫秒值
private?long?time;
public?void?run()?{
while?(true)?{
now?=?System.currentTimeMillis();//?獲取一秒之后的毫秒值
time?=?now?-?start;//?兩個(gè)時(shí)間相減的到毫秒差
System.out.format("%02d:%02d:%02d\n",
time?/?(1000?*?60?*?60)?%?60/*?時(shí)?*/,?
time?/?(1000?*?60)%?60/*?分?*/,?
time?/?1000?%?60/*?秒?*/);//?格式化字符串輸出
try?{
Thread.sleep(1000);
}?catch?(InterruptedException?e)?{
e.printStackTrace();
}
}
}
}
import java.awt.BorderLayout;import java.awt.Container;import java.awt.Font;import java.awt.GridLayout;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import javax.swing.JButton;import javax.swing.JFrame;import javax.swing.JOptionPane;import javax.swing.JPanel;import javax.swing.JTextField;public class TimerDemo extends JFrame implements ActionListener { private static final long serialVersionUID = 201306211111L; private JTextField screen = new JTextField("0"); private JButton start = new JButton("開始"); private JButton reset = new JButton("重置"); private JPanel panel = new JPanel(); private boolean isRunning; private int time; private int timeBetween; public TimerDemo(int timeBetween) { super("計(jì)時(shí)器"); this.timeBetween = timeBetween; try { init(); } catch (Exception e) { e.printStackTrace(); } } public TimerDemo() { super("計(jì)時(shí)器"); this.timeBetween = 100; try { init(); } catch (Exception e) { e.printStackTrace(); } } private void init() { panel.setLayout(new GridLayout()); panel.add(start); panel.add(reset); start.addActionListener(this); reset.addActionListener(this); screen.setFont(new Font("幼圓", Font.BOLD, 60)); screen.setHorizontalAlignment(JTextField.CENTER); screen.setEditable(false); Container c = getContentPane(); c.setLayout(new BorderLayout()); c.add(panel, BorderLayout.SOUTH); c.add(screen, BorderLayout.CENTER); this.setSize(200, 150); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); this.setResizable(false); this.setLocationRelativeTo(null); this.setVisible(true); } public static void main(String[] args) { new TimerDemo(1);// 設(shè)定 1ms/次 // new TimerDemo(); } @Override public void actionPerformed(ActionEvent e) { if (e.getSource() == start) { if (start.getText().equals("開始")) { start.setText("暫停"); isRunning = true; } else if (start.getText().equals("暫停")) { start.setText("開始"); isRunning = false; } } if (e.getSource() == reset) { start.setText("開始"); screen.setText("0"); isRunning = false; time = 0; } new Thread(new TimeZone()).start(); } class TimeZone implements Runnable { @Override public void run() { while (isRunning) { time++; if (time = Integer.MAX_VALUE) { screen.setText("ERROR"); JOptionPane.showMessageDialog(null, "ERROR"); isRunning = false; } screen.setText(String.valueOf(time)); try { Thread.sleep(timeBetween); } catch (Exception e) { e.printStackTrace(); } } } }}
你可以在開始和結(jié)束的時(shí)候,分別記錄下當(dāng)前的時(shí)間的這毫秒數(shù)。然后再減,以下是一段代碼。
public class Test{
public static void main(String[] args) {
long startMili=System.currentTimeMillis();// 當(dāng)前時(shí)間對(duì)應(yīng)的毫秒數(shù)
System.out.println("開始 "+startMili);
// 執(zhí)行一段代碼,求一百萬次隨機(jī)值
for(int i=0;i1000000;i++){
Math.random();
}
long endMili=System.currentTimeMillis();
System.out.println("結(jié)束 s"+endMili);
System.out.println("總耗時(shí)為:"+(endMili-startMili)+"毫秒");
}
}
新聞名稱:java代碼運(yùn)行計(jì)時(shí) java程序計(jì)時(shí)
網(wǎng)頁地址:http://jinyejixie.com/article38/doppgpp.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站制作、自適應(yīng)網(wǎng)站、定制網(wǎng)站、網(wǎng)站維護(hù)、用戶體驗(yàn)、企業(yè)建站
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)