重庆分公司,新征程启航
为企业提供网站建设、域名注册、服务器等服务
用java实现一个计时器的方法:
10余年的封丘网站建设经验,针对设计、前端、开发、售后、文案、推广等六对一服务,响应快,48小时及时工作处理。成都全网营销推广的优势是能够根据用户设备显示端的尺寸不同,自动调整封丘建站的显示方式,使网站能够适用不同显示终端,在浏览器中调整网站的宽度,无论在任何一种浏览器上浏览网站,都能展现优雅布局与设计,从而大程度地提升浏览体验。成都创新互联公司从事“封丘网站设计”,“封丘网站推广”以来,每个客户项目都认真落实执行。
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定时在此
}
}
}
计时器可以使用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();
}
}
}
}
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("计时器"); this.timeBetween = timeBetween; try { init(); } catch (Exception e) { e.printStackTrace(); } } public TimerDemo() { super("计时器"); 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);// 设定 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(); } } } }}
你可以在开始和结束的时候,分别记录下当前的时间的这毫秒数。然后再减,以下是一段代码。
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)+"毫秒");
}
}