在线程

时间:2016-03-07 20:20:06

标签: java multithreading

一种新的线程化。

我目前的代码;

public boolean running = false;
private void jStartButton1ActionPerformed(java.awt.event.ActionEvent evt){
     running = true;
     (new Thread(new Home())).start();

}    
private void jStopButton2ActionPerformed(java.awt.event.ActionEvent evt) {                                         
   running = false;
   System.out.println("trying to stop");
}  


public void run() {
   while(running){
       //continously run code from a file thats updated every few seconds, running=false when its found what its looking for
   }

}

正在处理jStopButton2并且在调试中将运行设置为false,但它从未被run()线程识别。 run()有自己的running = false,当它找到它寻找的内容时(并且它会停止while循环),但是现在如果我想停止,我就无法手动停止它。

我知道我的线程错了,有什么帮助吗?可能是线程中的所有内容都被锁定了吗?所以它没有认识到运行的bolean被改变了吗?我该如何改变呢? o.o

非常感谢任何帮助。

2 个答案:

答案 0 :(得分:1)

此时您正在创建Home类的新实例

(new Thread(new Home())).start();

因此,您的旗帜running将再次初始化(如果它不是静态的) 因此,尝试使用相同的House实例,并保留running

的值

我的意思是

(new Thread(this)).start();

答案 1 :(得分:0)

这是一个非常简单的同步应用程序(基于您使用running标志的代码),它从主线程启动第二个线程(您的Home类),稍等一下,然后决定停止第二个线程。它为第二个线程提供了完成其工作和/或观察running标志已经更改的时间,最后也完成了主线程。

import java.util.*;
import java.lang.*;
import java.io.*;

class Main
{
    public static void main (String[] args) throws java.lang.Exception
    {
        Home home = new Home();
        Thread thread = home.jStartButton1ActionPerformed(null);

        System.out.println("main thread sleeping");
        Thread.sleep(3000);

        System.out.println("main thread deciding to stop secondary thread...");
        home.jStopButton2ActionPerformed(null);

        System.out.println("main thread waiting for secondary thread to finish...");
        thread.join();

        System.out.println("main thread finished");
    }
}

class Home implements Runnable
{
    public boolean running = false;

    public Thread jStartButton1ActionPerformed(java.awt.event.ActionEvent evt){
        running = true;

        Thread thread = new Thread(this);
        thread.start();

        System.out.println("secondary thread started");

        // Returning thread instance so that the main thread can synchronize with it (wait for it to finish)
        return thread;
    }

    public void jStopButton2ActionPerformed(java.awt.event.ActionEvent evt) {                                         
        running = false;
        System.out.println("trying to stop");
    }

    public void run() {
        while(running){
            //continously run code from a file thats updated every few seconds, running=false when its found what its looking for
            // Simulating work by sleeping.
            try {
                Thread.sleep(500);
            } catch (Exception e) {}
            System.out.println("secondary thread running...");
        }
        System.out.println("secondary thread stopped");
    }
}

您的示例中的问题是您每次都在执行new Home(),从而创建一个您无法控制的新的可运行实例(具有自己的running版本)。在上面的示例中,当前的runnable实例是启动线程的实例,因此它确保将自己设置为线程要执行的代码,从而保留对running字段的控制。主线程控制Home实例,以便在running实例上调用stop方法时控制Home字段。