Java 如何访问正在运行的线程/runnable?
我有一个线程正在运行,但从外部我无法绕过一个值来停止该线程。如何在Java 如何访问正在运行的线程/runnable?,java,Java,我有一个线程正在运行,但从外部我无法绕过一个值来停止该线程。如何在Mytest()内发送假/真值或调用运行线程公共方法?当我按下按钮1时? 例如:thread.interrupt()runnable.stop()或runnable.start() 跟进(校对): Step 1: /* Main - boot/startup */ public class Main extends JFrame { public static Mytest runnable; // wrong: p
Mytest()
内发送假/真值或调用运行线程公共方法?当我按下按钮1时?
例如:thread.interrupt()代码>runnable.stop()
或runnable.start()代码>
跟进(校对):
Step 1:
/* Main - boot/startup */
public class Main extends JFrame
{
public static Mytest runnable; // wrong: public static Runnable runnable;
public static Thread thread;
private JButton b1 = new JButton("Start");
private JButton b2 = new JButton("Stop");
public void init()
{
// Execute a job on the event-dispatching thread:
// In case Freezed for heavy lifting
try {
javax.swing.SwingUtilities.invokeAndWait(new Runnable()
{
public void run()
{
createGUI();
}
});
} catch (Exception e) {
System.err.println("createGUI didn't successfully complete");
}
}
public void createGUI()
{
Container cp = getContentPane();
b1.addActionListener(new button1());
cp.add(b1);
runnable = new Mytest();
thread = new Thread(runnable);
try {
thread.sleep(100); // value is milliseconds
thread.start();
} catch (InterruptedException e) {
}
}
public static void main(String[] args)
{
run(new Main(), 500, 500);
}
public static void run(JFrame frame, int width, int height)
{ ...
frame.setVisible(true);
}
}
/* To start */
public class button1 implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
runnable.start();
}
}
/* To stop */
public class button2 implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
runnable.stop();
}
}
Step 2:
/* Thread deals */
public class Mytest implements Runnable
{
private static volatile boolean running = true;
public void run()
{
while(running)
{
// do stuff
}
}
public void start() { running = true; }
public void stop() { running = false;}
}
如果您按类而不是按Runnable
定义它,则可以调用实例方法
public static Mytest runnable;
还要注意的是,由于多个内核都有自己的关联内存,因此需要警告处理器,另一个处理器上的状态可能会发生更改,并且需要监视该更改。听起来很复杂,但只需将“volatile”关键字添加到布尔标志中即可
public class Mytest implements Runnable
{
private static volatile boolean running = true;
public void run()
{
while(running) {
// do stuff
}
}
public void stop() { running = false;}
}
在初始代码中启动Runnable
,然后使用Runnable.stop()
将其关闭(除了此线程是CPU的加热测试之外;)
您可以使用调用启动/停止方法
MyThread.start();
MyThread.stop();
您已经将它们定义为static
方法,因此上面的代码行显示了如何调用它们
取暖。。。添加如下内容
try {
Thread.sleep(100); // value is milliseconds
} catch (InterruptedException e) {
// no need to handle (in this example)
}
这将把CPU负载从100%(在一个内核上)降低到一个合理的值;) 在您的跑步方法中
不要做的时候(真的)
使用布尔值。。。喜欢while(线程正在运行)
这个布尔值可以设置为true/false
public void run()
{
while(!isInterrupted()) {
if (onoff) {
return;
} else {
if (status==false) System.out.println("running");
}
}
}
然后使用Thread.interrupt()指示线程的中断
注意:在任何情况下都不要使用Thread.stop()!它已被弃用
有关更多详细信息,请参阅JDK文档和>。您应该始终使用中断方法停止线程。这是对线程执行停止操作的安全且适当的方法
Thread tThread = new Thread(new Runnable() {
public void run() {
while (!Thread.currentThread().isInterrupted()) {
try{
Thread.sleep(10);
... do you stuff...
}catch(InterruptedException ex){
break;
}
}
}
});
tThread.start();
当您想停止线程时,只需调用中断方法:
tThread.interrupt();
是的,但它必须是一个原子布尔值
,因为a)它必须是最终的,b)这样在调用runnable.stop()时可以保证线程同步,它是否只停止循环?但实例仍然没有关闭?在这个代码模式中?然后Thread.stop()或Thread.interrupt()需要终止池?当run
方法结束时,线程停止。如果需要,您可以将Mytest实例附加到新线程,或者如果要再次运行它,可以在现有线程上调用start()
。此代码只是确保正在运行的代码将在合理的时间范围内完成(如果标志不可变,则情况并非如此),谢谢!!请参阅上面的最新更新。因此,您的意思是,如果我们将while循环标记为stop标志,它会自动释放线程,如果我们将其标记为start(),它会重新创建线程?new Runnable()
只是创建匿名类的一种方便方法,该类实现Runnable而不显式声明类。如果要在其他位置访问该类,请将其声明为实现Runnable的类,并具有相同的方法,然后在代码中创建一个实例,用于执行其他操作;这是阻止它的正确方法。它将等待任何睡眠或操作完成,以便在您启动它时能够重新创建它。
tThread.interrupt();