定时器更新界面,线程报错

项目场景:

在javafx框架下使用线程更新UI的时候,出现无法正常更新UI。

问题代码如下:

package clock;

import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.Timer;
import java.util.TimerTask;

import javafx.application.Application;
import javafx.application.Platform;
import javafx.scene.Scene;
import javafx.stage.Stage;

public class SimpleClock extends Application{

    public static void main(String[] args) {
        Application.launch();
    }
    public void start(Stage stage) {
        
        Timer timer = new Timer();
        timer.schedule(new TimerTask() {
            @Override
            public void run() {
                
                
                        init(stage);
                        
                    
                });
            }
        }, 0, 1000); 
    
    }

    private void init(Stage stage){
        Calendar calendar = new GregorianCalendar();
        int hour = calendar.get(Calendar.HOUR_OF_DAY);
        int minute = calendar.get(Calendar.MINUTE);
        int second = calendar.get(Calendar.SECOND);
        ClockPane cPane = new ClockPane(hour, minute, second);
        Scene scene = new Scene(cPane, 300, 300);
        stage.setTitle("Watch");
        stage.setScene(scene);
        stage.show();
    }

    
}

问题描述

运行程序之后,出现如下报错:

Exception in thread "Timer-0" java.lang.IllegalStateException: Not on FX application thread; currentThread = Timer-0
        at javafx.graphics@22.0.1/com.sun.javafx.tk.Toolkit.checkFxUserThread(Toolkit.java:294)
        at javafx.graphics@22.0.1/com.sun.javafx.tk.quantum.QuantumToolkit.checkFxUserThread(QuantumToolkit.java:481)
        at javafx.graphics@22.0.1/javafx.stage.Stage.setScene(Stage.java:269)
        at clock.SimpleClock.init(SimpleClock.java:34)
        at clock.SimpleClock$1.run(SimpleClock.java:23)
        at java.base/java.util.TimerThread.mainLoop(Timer.java:571)
        at java.base/java.util.TimerThread.run(Timer.java:521)

原因分析:

checkFxUserThread

从出错日志看出checkFxUserThread这个方法在检查用户进程是否为UI线程,如果不是会抛出异常,这是因为为了使UI渲染不出现阻塞现象,不允许在子线程中进行UI操作。


解决方案:

可以使用javafx框架提供的runlater方法,实现切换到UI线程来执行更新UI的操作。

    public void start(Stage stage) {
        
        Timer timer = new Timer();
        timer.schedule(new TimerTask() {
            @Override
            public void run() {
                
                Platform.runLater(new Runnable() {
                    @Override
                    public void run() {
                        init(stage);
                        
                    }
                });
            }
        }, 0, 1000); 
    
    }

  • 4
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
C#中定时器和多线程是常用的并发编程工具。定时器用于在指定的时间间隔内执行某个任务,而多线程则允许同时执行多个任务。 在C#中,可以使用Timer类来创建定时器。Timer类有两种类型:System.Timers.Timer和System.Threading.Timer。System.Timers.Timer是基于事件的定时器,适用于Windows Forms和WPF应用程序。System.Threading.Timer是基于线程池的定时器,适用于所有类型的应用程序。 下面是一个使用System.Timers.Timer的示例: ```csharp using System; using System.Timers; class Program { static void Main() { Timer timer = new Timer(1000); // 创建一个1秒的定时器 timer.Elapsed += TimerElapsed; // 绑定定时器事件 timer.Start(); // 启动定时器 Console.WriteLine("按任意键停止定时器..."); Console.ReadKey(); timer.Stop(); // 停止定时器 } static void TimerElapsed(object sender, ElapsedEventArgs e) { Console.WriteLine("定时器触发,当前时间:{0}", e.SignalTime); } } ``` 多线程在C#中使用Thread类来创建和管理线程。以下是一个使用多线程的示例: ```csharp using System; using System.Threading; class Program { static void Main() { Thread thread = new Thread(DoWork); // 创建一个新线程 thread.Start(); // 启动线程 Console.WriteLine("按任意键停止线程..."); Console.ReadKey(); thread.Abort(); // 终止线程 } static void DoWork() { while (true) { Console.WriteLine("线程执行中..."); Thread.Sleep(1000); // 暂停1秒 } } } ``` 以上是使用定时器和多线程的基本示例,你可以根据自己的需求进行定制和扩展。请注意,在使用多线程时要小心处理线程安全性和资源共享的问题。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值