【问题标题】:How call another thread run method from Jform如何从 Jform 调用另一个线程运行方法
【发布时间】:2015-06-15 22:53:56
【问题描述】:

当我执行按钮点击事件时,它不能调用其他,这个类是由 Runnable 接口实现的。在这里,我的问题是在我通过此按钮单击事件调用时执行这些类需要更多时间。但是可运行接口类的正常执行速度非常快。任何人都可以给我解决这些问题的方法。

【问题讨论】:

    标签: java multithreading thread-safety buttonclick jform


    【解决方案1】:

    你可以从你的 JFrame 开始一个新线程:

    Thread th=new Thread(yourRunnableClass);
    th.start();//This will run your `run` method in a seperate thread.
    

    如果您的线程将执行与 GUI 相关的操作,那么您应该按如下方式运行您的线程。因为 Swing 应用程序的 GUI 应该在单个线程中运行。 Runnable接口会在应用空闲时运行。

    SwingUtilities.invokeLater(yourRunnableClass);
    

    【讨论】:

      猜你喜欢
      • 2012-07-01
      • 2012-04-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-12-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多