简体   繁体   English

如何从Jform调用另一个线程运行方法

[英]How call another thread run method from Jform

When I performed button click event, it can't calling other and this class is implemented by Runnable interface. 当我执行按钮单击事件时,它无法调用其他事件,并且此类由Runnable接口实现。 Here my problem is execution of these class taking more time while I calling through this button click event. 在这里,我的问题是在我通过此按钮click事件调用时,这些类的执行需要更多时间。 But normal execution of runnable interface class is executing very fast. 但是可运行接口类的正常执行速度非常快。 Can any give me solution get out from these problem. 任何可以给我解决方案的人都可以摆脱这些问题。

You can start a new thread from your JFrame : 您可以从JFrame启动一个新线程:

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

If your thread will do something related to GUI then you should run your thread as below. 如果您的线程将执行与GUI相关的操作,则应按以下方式运行线程。 Because GUI of a Swing application should run in a single thread. 因为Swing应用程序的GUI应该在单个线程中运行。 The Runnable interface will run when the application is idle. 当应用程序空闲时, Runnable接口将运行。

SwingUtilities.invokeLater(yourRunnableClass);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM