java - 如何访问正在运行的线程/runnable?

标签 java

我有一个正在运行的线程,但从外部我无法绕过一个值来停止该线程。如何在 Mytest() 中发送 false/true 值或调用正在运行的线程公共(public)方法?当我按下按钮 1? 例如:thread.interrupt(); runnable.stop();runnable.start();

// Main
public class Main extends JFrame
{
  public static Runnable runnable;
  public static Thread thread;
  private JButton b1    = new JButton("Start/Stop");

  public void init() 
  {    
    //Execute a job on the event-dispatching thread:
    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);
    thread.start();
  }
}

// Button 1 - [problem to go  inside a running thread]
public class button1 implements ActionListener 
{
  public void actionPerformed(ActionEvent e) 
  {
    System.out.println("button pressed - need to access ");
      //thread.interrupt();       runnable.stop(); //or runnable.start();
  }
}

// Running - Thread
public class Mytest implements Runnable
{
  public static boolean onoff = false;
  public static boolean status = false;

  public void run()
  {
    while(true) 
    {
      if (onoff) 
      {
         return;
       } else { 
         if (status==false) System.out.println("running"); 
       }
    }
  }
  public static void stop() { status = true; onoff=true; }
  public static void start() { status = false; onoff = false; }
}

跟进(校对):

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' 关键字添加到 boolean 标志

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() 关闭它

关于java - 如何访问正在运行的线程/runnable?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5562720/

相关文章:

java - SLF4J的多重绑定(bind)

java - Spring - 如何获取属性文件的子集而不枚举所有必需的属性

Java 从操纵杆(如 Xbox Controller )获取输入,无需使用外部 api

java - Embeddable 中枚举的 @ElementCollection

Java EnumSet 语法帮助

java - 使用 OPEN SAML 的服务提供商实现 - JAVA

java - 一次为所有包导入 Java 类

java - 为什么我的字母字符出现频率为 0?

java - 比较 2 个列表中的元素时如何避免 O(N^2)?

java - 从命令行绑定(bind)变量来测试 groovy 脚本