java线程计时器代码 计时器java编程

做一个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编写一个倒计时器代码 。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();}@Overridepublic 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 {@Overridepublic 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();}}}}}
java计时器何不使用线程?
一些说明我已经写在注释中了
public class Test implements Runnable {
//定义一个线程
Thread thread;
//用于停止线程的标志
private boolean flag=true;
public Test(){
thread=new Thread(this);
}
//因为该类实现了Runnable,就必须有run方法,当线程启动时,会调用这个run方法
public void run(){
//获得当前的时间 , 毫秒为单位
long beginTime=System.currentTimeMillis();
//定义已过去的时间
long time=0;
while(flag){
//这里写实现计时的代码
//在这里,获得已过去的时间
time=System.currentTimeMillis()-beginTime;
System.out.println("已过" time "毫秒");
//暂停线程1秒钟,不暂停的话可以把下面代码去掉
try{
Thread.sleep(1000);
}catch(InterruptedException e1){
e1.printStackTrace();
}
}
}
//这里是启动线程的方法,也就是启动线程
public void start(){
thread.start();
}
//这里是暂停的方法,暂停线程
public void Pause(){
try{
thread.wait();
}catch(InterruptedException e){
e.printStackTrace();
}
}
//这里是继续的方法,唤醒线程
public void Resume(){
thread.notifyAll();
}
//停止线程
public void stop(){
//把flag设成false,则在run中的while循环就会停止循环
flag=false;
}
public static void main(String []args){
//用于测试
Test t=new Test();
//开始线程
t.start();
try{
//10000毫秒以后结束线程
Thread.sleep(10000);
}catch(InterruptedException e1){
e1.printStackTrace();
}
//结束线程
t.stop();
}
}
求java的计时器代码,应该比较简单的 , 来看看吧 。package test;
import java.util.*;
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
public class Test5 extends Applet {
private final Panel pan = new Panel();
private final Label time = new Label();
private final Button btnGo = new Button("开始");
private final Button btnPouse = new Button("暂停");
private final Button btnReset = new Button("复位");
private final StopwatchThread swThread = new StopwatchThread();
private class btnGoListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
swThread.go();
btnGo.setEnabled(false);
}
}
private class btnPouseListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
if(btnGo.isEnabled()){
return ;
}
if (btnPouse.getLabel().equals("继续")) {
swThread.go();
btnPouse.setLabel("暂停");
} else if (btnPouse.getLabel().equals("暂停")) {
swThread.noGo();
btnPouse.setLabel("继续");
}
}
}
private class btnResetListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
swThread.reset();
btnGo.setEnabled(true);
btnGo.setLabel("开始");
btnPouse.setLabel("暂停");
}
}
private class StopwatchThread extends Thread {
private boolean going = false;
private long prevElapsed = 0;
private Date startDate = new Date();
private long elapsedTime() {
return prevElapsed
(going ? new Date().getTime() - startDate.getTime() : 0);
}
private String msToString(long time) {
System.out.println(time "" ((0*60 2)*1000 999));
if(((99*60 59)*1000 983)=time((99*60 59)*1000 999)=time){//((0*60 2)*1000 983)=time((0*60 2)*1000 999)=time
if (time % 1000990)
time= 2;
swThread.noGo();
}
String ms, sec, min;
if (time % 10 = 5)
time= 5;
ms = Long.toString(time % 1000);
while (ms.length()3)
ms = "0"ms;
ms = ms.substring(0, ms.length() - 1);
time /= 1000;
sec = Long.toString(time % 60);
if (sec.length() == 1) sec = "0"sec;
time /= 60;
min = Long.toString(time);
return min":"sec"."ms;
}
public void go() {
startDate = new Date();
going = true;
}
public void noGo() {
prevElapsed = elapsedTime();
going = false;
}
public void reset() {
going = false;
prevElapsed = 0;
}
public void run() {
while (true) {
time.setText(msToString(elapsedTime()));
yield();
}
}
}
public void init() {
setLayout(new GridLayout(2,2));
setBackground(Color.lightGray);
setForeground(Color.black);
pan.setLayout(new GridLayout(3,2));
pan.add(new Label("计时:"));
time.setForeground(Color.blue);
pan.add(time);
pan.add(btnGo);
pan.add(btnPouse);
pan.add(btnReset);
pan.add(new Label());
add(pan);
btnGo.addActionListener(new btnGoListener());
btnReset.addActionListener(new btnResetListener());
btnPouse.addActionListener(new btnPouseListener());
swThread.setDaemon(true);
swThread.start();
}
public static void main(String[] args) {
Test5 applet = new Test5();
Frame aFrame = new Frame("计时器");
aFrame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
aFrame.add(applet, BorderLayout.CENTER);
aFrame.setSize(400, 200);
applet.init();
applet.start();
aFrame.setVisible(true);
}
}
可以改变有注释java线程计时器代码的那个if语句里面java线程计时器代码的值来判断什么时候停止
【java线程计时器代码 计时器java编程】关于java线程计时器代码和计时器java编程的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站 。

    推荐阅读