java 多线程定时器(整点报时)
实现整点报时的详细步骤
第一 构建时间类
1.构建一个时间类 作为锁 和判断
public class Time { public boolean flag; // 默认false 判断public Time() {super();}}
第二 构建一直持续输出时间的类
1.记录时间是用的Calendar类 方便处理时间的各个部分
2.传入的 JTextArea 是一个GUI实现的一个文本框 在这里显示时间
3.各个时间值 使用 StringBuffer 拼接起来
4.每次线程都休眠一秒,代表时间一秒就一次刷新时间
5.flag 是用来判断的如果到达整点 依旧输出改时间点 但是 flag改为false
6.使得下面的SetTime类实现报时的效果
import java.util.Calendar;import javax.swing.JTextArea;public class GetTime implements Runnable {private Time t; //设置判断和作为一把对象锁private JTextArea textArea; //文本域显示当前时间public GetTime(Time t,JTextArea textArea) {super();this.t = t;this.textArea=textArea;}public void run() { //使时间都拼接成字符串的形式StringBuffer sb = new StringBuffer();//String s = "sss";String str = null;synchronized(t) {while(true) {//获取当前时间Calendar c = Calendar.getInstance(); //可以对每个时间域单独修改int year = c.get(Calendar.YEAR);int month = c.get(Calendar.MONTH)+1;int date = c.get(Calendar.DATE);int hour = c.get(Calendar.HOUR_OF_DAY);int minute = c.get(Calendar.MINUTE);int second = c.get(Calendar.SECOND);//如果没有整点 就继续显示if(!t.flag) {str = sb.append(year).append("年").append(month).append("月").append(date).append("日").append(hour).append("时").append(minute).append("分").append(second).append("秒").toString();textArea.setText(str);}else{//改变flag状态t.flag=false;}try {Thread.sleep(1000);//清除sb缓冲区的字符串 sb.delete(0, str.length());} catch (InterruptedException e) {// TODO Auto-generated catch blocke.printStackTrace();}}}}}
第三构建等待整点就报时的时间的类
1.这是一个就是实现整点报时的类
2.如果到了整点,就显示报时,不显示时间,当不是整点是又恢复输出时间
import javax.swing.JLabel;public class SetTime implements Runnable {private Time t;private JLabel setTime;public SetTime(Time t,JLabel setTime) {super();this.t = t;this.setTime=setTime;}@Overridepublic void run() { while (true) {synchronized (new Object()) {//每次都是重新定义 可以免去删去一直拼接的情况//使时间都拼接成字符串的形式StringBuffer sb = new StringBuffer();String str = null;//获取当前的时间Calendar c = Calendar.getInstance();//可以对每个时间域单独修改int year = c.get(Calendar.YEAR);int month = c.get(Calendar.MONTH);int date = c.get(Calendar.DATE);int hour = c.get(Calendar.HOUR_OF_DAY);int minute = c.get(Calendar.MINUTE);int second = c.get(Calendar.SECOND);//setTime.setForeground(Color.BLACK);//如果没有整点就不显示setTime.setText(" ");//分,秒都等于0 时就是整点if(second==0&&minute==0) {t.flag=true;//改变flag的状态str = sb.append(hour).append("时").toString();//显示时间 并报时setTime.setText("整点报时"+str+"到了");//字体改为红色setTime.setForeground(Color.RED);}try {Thread.sleep(1000);//间隔一秒} catch (InterruptedException e) {// TODO Auto-generated catch blocke.printStackTrace();}}}}}
最后一步 使用GUI 实现
将整点定时器更好的体现
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.EventQueue;
import java.awt.Font;import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JLabel;
import javax.swing.JTextArea;
import javax.swing.event.AncestorListener;
import javax.swing.event.AncestorEvent;public class TimeJFrame extends JFrame {private JPanel contentPane;private Time t;private GetTime g;private SetTime ss;/*** Launch the application.*/public static void main(String[] args) {EventQueue.invokeLater(new Runnable() {public void run() {try {TimeJFrame frame = new TimeJFrame();frame.setVisible(true);} catch (Exception e) {e.printStackTrace();}}});}/*** Create the frame.*/public TimeJFrame() {//设置标题setTitle("整点报时");setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);setBounds(100, 100, 465, 300);contentPane = new JPanel();contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));setContentPane(contentPane);contentPane.setLayout(null);JLabel label = new JLabel("当前的时间是:");label.setBounds(14, 13, 125, 34);contentPane.add(label);JTextArea textArea = new JTextArea();textArea.setBounds(14, 65, 428, 45);contentPane.add(textArea);//设置文本域的属性 字体 大小textArea.setFont(new Font("黑体",Font.BOLD,32));t=new Time();//调用GetTime类g=new GetTime(t,textArea);//开始线程new Thread(g).start();JLabel setTime = new JLabel();setTime.setBounds(22, 164, 420, 59);contentPane.add(setTime);//设置标签的属性 字体 大小setTime.setFont(new Font("黑体",Font.BOLD,40));//setTime.setForeground(Color.RED);//显示空格setTime.setText(" ");//调用SetTime类 并开始线程ss=new SetTime(t,setTime);new Thread(ss).start();}
}
效果图
源码:https://github.com/kangz666/-