4

java上位机开发(定时器使用)

 2 years ago
source link: https://blog.csdn.net/feixiaoxing/article/details/125713992
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

java上位机开发(定时器使用)

嵌入式-老费 于 2022-07-11 07:07:39 发布 19

【 声明:版权所有,欢迎转载,请勿用于商业用途。 联系信箱:feixiaoxing @163.com】

        定时器是实际开发中常用的一个特性。在实际开发中,我们发现有一些任务需要周期性的完成,比如说数据库的检查、设备的检查、状态的检查、变量的检查等等。这个时候就会使用到定时器。在java swing中,因为本身已经集成了定时器,所以直接使用即可。

        需要注意的是,定时器的回调函数中,对相关变量的访问和改写有可能与控件中的回调函数有冲突。这个时候,就需要做好数据的互斥保护工作。

1、准备测试代码

2、代码分析

        代码主要来自于https://www.delftstack.com/zh/howto/java/swing-timer-in-java/这个地址,略作修改,在此表示感谢。代码中定义了一个变量timer,回调函数就是ActionListener中actionPerformed函数。timer设置为可重复式出发、开始、主线程睡眠、结束。

3、编译、执行

        代码没有问题的话,执行后你会看到这样的结果,


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK