深度讲解Java多线程开发

您所在的位置:网站首页 hoops电子表 深度讲解Java多线程开发

深度讲解Java多线程开发

2024-07-14 15:33| 来源: 网络整理| 查看: 265

目录

1、前端界面的设计

2、添加控件的事件监听

3、通过主线程对时间实时显示

4、启用线程实现对时间的修改

5、线程同步实现秒表功能

6、运行并进行调试

Hello,大家好,我是灰小猿!

今天和大家分享一个使用Java多线程开发的电子表项目,可以实现电子表中时间的实时显示,修改以及秒表的功能。

Java电子表设计的设计顺序为从前端界面到后端类及线程的设计,之后将前后端相结合而成。以下是电子表的开发过程:  

1、前端界面的设计

电子表的前端界面设计依据JFrame窗体和Container容器,采用绝对定位的方法对时间显示、时间修改、秒表显示等控件进行合理布局设计,力求界面美观简洁。

2、添加控件的事件监听

在进行界面设计完成之后进行的工作是对相应的控件添加函数监听,在这里调用的是ActionListener接口,并且重写其中的actionPerformed方法,在其中对“确认修改”、“启动秒表”、“暂停”这三个按钮添加监听,并且在相应的监听中添加事件,以至于在点击按钮时候可以触发相应的事件。以下是对actionPerformed方法的重写

@Override public void actionPerformed(ActionEvent e) { // 如果点击了确认修改按钮 if (e.getSource() == amend_JB) { //获取到下拉框的值 String hour_amend = hourAmend.getSelectedItem().toString(); String minute_amend = minuteAmend.getSelectedItem().toString(); String second_amend = secondAmend.getSelectedItem().toString(); //JOptionPane.showMessageDialog(null, "修改成功!"); isThreadShow = false; //设置线程标记为False,中止线程 //将修改的值进行显示 hourShow.setText(hour_amend); minuteShow.setText(minute_amend); secondShow.setText(second_amend); System.out.println("修改的时间是:" + hour_amend + ":" + minute_amend + ":" + second_amend); threadAmend.start();//启动修改后运行时间的线程 } //如果点击了启动秒表的按钮 if (e.getSource() == second_JB) { //如果当前秒表是启动状态,也就是显示的是让停止计时 if (second_JB.getText() == "停止计时") { second_JB.setText("启动秒表"); second_JB.setBackground(Color.yellow); //threadSecond.stop(); isStartSecond = false; } else { //如果当前秒表是关闭状态 second_JB.setText("停止计时"); second_JB.setBackground(Color.RED); threadSecond.start();//启动秒表线程 isStartSecond = true; } } //如果点击了暂停按钮 if (e.getSource() == pause_JB) { if (pause_JB.getText() == "暂停") { pause_JB.setText("继续"); pause_JB.setBackground(Color.cyan); threadSecond.suspend(); //使该线程暂停 } else { pause_JB.setText("暂停"); pause_JB.setBackground(Color.RED); threadSecond.resume(); //使该线程继续 } } } 3、通过主线程对时间实时显示

对按钮控件添加了监听函数之后,是对当前时间的显示,时间的显示是使用主线程,并且在主线程中每一秒更新显示一次数据,在这里使用的是Date类进行系统时间的读取,并且再利用SimpleDateFormat将获取到的时间进行规范化处理,之后将经过处理后得到的年、月、日、星期、以及当前的时间在界面中进行显示出来,

@Override public void run() { while (true) { // TODO Auto-generated method stub Date date = new Date(); //实例化时间类对象 //将当前获取到的时间规范化 SimpleDateFormat yearFormat = new SimpleDateFormat("yyyy"); SimpleDateFormat monthFormat = new SimpleDateFormat("MM"); SimpleDateFormat dayFormat = new SimpleDateFormat("dd"); SimpleDateFormat weekFormat = new SimpleDateFormat("EEEE"); SimpleDateFormat hourFormat = new SimpleDateFormat("HH"); SimpleDateFormat minuteFormat = new SimpleDateFormat("mm"); SimpleDateFormat secondFormat = new SimpleDateFormat("ss"); //获取到当前的年、月、日、时、分、秒 year = yearFormat.format(date); month = monthFormat.format(date); day = dayFormat.format(date); week = weekFormat.format(date); hour = hourFormat.format(date); minute = minuteFormat.format(date); second = secondFormat.format(date); hourShow.setText(hour); minuteShow.setText(minute); secondShow.setText(second); timeShow.setText(year + "年" + month + "月" + day + "日" + week); try { Thread.sleep(1000); //让线程每一秒执行一次 } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } //如果线程中止,跳出循环 if (!isThreadShow) { break; } } } 4、启用线程实现对时间的修改

在将系统时间显示出来之后,是对当前时间修改的操作,在这里的设计思路是,首先获取到修改的时间,然后利用break跳出主线程的循环,从而结束主线程的运行,之后将已经读取到的修改的时间在显示时间的标签中进行显示,同时开启修改后继续进行时间运行的threadAmend线程,并且每一秒更新一次,以至于时间可以不断的在已经修改过的基础上继续执行。效果如下图:  

//设置threadAmend线程 threadAmend = new Thread(new Runnable() { @Override public void run() { while (true) { int hourAmend = Integer.parseInt(hourShow.getText()); int minuteAmend = Integer.parseInt(minuteShow.getText()); int secondAmend = Integer.parseInt(secondShow.getText()); secondAmend++; //对获取到的时间进行判断并作出处理 if (secondAmend == 60) { minuteAmend++; secondAmend = 0; } if (minuteAmend == 60) { hourAmend++; minuteAmend=0; } if (hourAmend == 24) { hourAmend = 0; minuteAmend = 0; secondAmend = 0; } //将时间进行显示 hourShow.setText(Integer.toString(hourAmend)); minuteShow.setText(Integer.toString(minuteAmend)); secondShow.setText(Integer.toString(secondAmend)); try { threadAmend.sleep(1000); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } }); 5、线程同步实现秒表功能

在修改时间的同时,会设定进行秒表运行的threadSecond线程,该线程的使用是在点击“启动秒表”按钮之后启动该线程,同时在后台开始计时,每秒对数据更新一次,之后在“暂停”按钮中添加事件,在点击“暂停”之后可以将threadSecond暂停,同时此按钮变成“继续”,在点击继续之后,threadSecond线程会继续执行,直到点击了“停止计时”之后,该线程将会跳出循环中止。效果如下图:

//设置threadSecond秒表线程 threadSecond = new Thread(new Runnable() { @Override public void run() { //在每次开启秒表时,将数据初始化为0 hourSecond = 0; minuteSecond = 0; secondSecond = 0; while (true) { secondSecond++; //秒数加1 //对获取到的时间进行判断并作出处理 if (secondSecond == 60) { minuteSecond++; secondSecond = 0; } if (minuteSecond == 60) { hourSecond++; minuteSecond=0; } String SecondText = Integer.toString(hourSecond) + ":" + Integer.toString(minuteSecond) + ":" + Integer.toString(secondSecond); second_Time.setText(SecondText); try { threadSecond.sleep(1000); //让线程每次休息一秒 } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } //如果监控秒表线程的StartSecond为falsh,跳出循环 if (!isStartSecond) { isStartSecond = true; break; } } } }); 6、运行并进行调试

在最后的秒表线程完成之后,整个电子表的开发就完成了,之后在主函数中直接调用该类即可运行成功。

public static void main(String[] args) { ElectronicClock eClock = new ElectronicClock(); }

效果如下:

觉得不错记得点赞关注哟!

完整源码点击此处即可下载,

或源码链接:https://pan.baidu.com/s/1Zc1XEp2tSDM7nKOykPEKDQ

提取码:u2co



【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3