当前位置: 首页 > 面试题库 >

如何访问正在运行的线程/可运行线程?

甘明朗
2023-03-14
问题内容

我有一个正在运行的线程,但是从外面我无法绕过一个值来停止该线程。如何在内部发送false /
true值Mytest()或调用运行线程的公共方法?当我按下按钮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;}
}

问题答案:

如果您通过类而不是通过a定义它,则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在您的初始代码中启动as,然后使用关闭它runnable.stop()



 类似资料:
  • 问题内容: 你好。我有一个实现runnable的类,并且有一个List,用于存储用该类的不同对象实例化的线程。给定运行线程对象的基础对象,如何访问基础对象的属性?这是一个例子: 问题答案: 我在文档中看不到任何方法。 那么,我最好的答案是您可能应该使用而不是(或在之上)使用。或者,也许您想要某种映射结构,以便可以从线程访问Runnable。(例如,)

  • 问题内容: 目的是能够从 主类中* 调用 单独线程的 执行。 * 一些上下文:我有一个必须运行 进程的程序 。仅当主程序执行完毕并从内存中卸载时,该进程 (一个cmd程序)才 应运行。 我应该在 主类中 包括哪些代码? 问题答案: 如果您的意思是: 如何启动一个Java线程,当我的JVM(Java程序)运行时,该线程不会结束? 。 因为在Java中,如果JVM退出,则所有线程均已完成。这是一个例子

  • 我有一个ScheduleTimer类,它可以处理日期数组。这是: 如果我像Java应用程序一样运行它,而不是像android一样运行,并且它在控制台中每隔一秒打印一次,那么它就可以正常工作。但是当在android环境中运行它时,它要么说UI线程不能从任何其他线程接触,要么它在类ScheduleTimer的方法中给了我null点异常。 我这样使用它:

  • QUUKUS版本2.1.2.最终的 在quarkusDev中运行时,一切都在正确的线程(vert.x-worker-thread-x)中运行,甚至遵守属性,如我所料。 当运行从构建任务构建的quokus-app时,我看到执行线程为(执行器-线程-x)。 不确定为什么执行线程在运行应用程序时发生变化,而不是在quakusDev中运行。顺便说一句,我使用了完全相同的属性,无论是夸克开发和运行构建任务的

  • 我用的是Netty camel-Netty:jar:2 . 10 . 0 . red hat-60024。下面是我对Netty监听器的配置 荨麻:tcp://10.1.33.204:9001?textline=true 在这里,我看到基于调试日志,Netty只创建一个工作线程,所以传入的消息被阻塞,直到现有的消息被处理。 喜欢: 2014-08-23 12:36:48,394|DEBUG|w I/

  • 本文向大家介绍C语言如何正确的终止正在运行的子线程,包括了C语言如何正确的终止正在运行的子线程的使用技巧和注意事项,需要的朋友参考一下 最近开发一些东西,线程数非常之多,当用户输入Ctrl+C的情形下,默认的信号处理会把程序退出,这时有可能会有很多线程的资源没有得到很好的释放,造成了内存泄露等等诸如此类的问题,本文就是围绕着这么一个使用场景讨论如何正确的终止正在运行的子线程。其实本文更确切的说是解