【问题标题】:wait call for a particular thread等待特定线程的调用
【发布时间】:2012-04-14 03:20:57
【问题描述】:

是否可以为另一个线程而不是当前线程调用等待方法。我要问的是这样的:

代码:

public class a extends JApplet{
 JButton start= new JButton("Start");
 JButton wait= new JButton("Wait");
 JButton notify = new JButton("Notify");
 final Thread bthread = new Thread(new B(), "BThread");

 @Override
 public void init(){
    //start
   this.getContentPane().setLayout(new FlowLayout()); 
   start.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            System.out.println("Started");
        }
    });
   this.getContentPane().add(start);
   //wait
   wait.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            System.out.println("Waited");
            synchronized(bthread)       //something like this
            {
                try {
                    bthread.wait();     //is it possible instead of the current thread the bthread get invoke
                 } catch (Exception ex) {
                    Logger.getLogger(a.class.getName()).log(Level.SEVERE, null, ex);
                }}
        }
    });
   this.getContentPane().add(wait);

   //notify
   notify.addActionListener(new ActionListener() {
         @Override
        public void actionPerformed(ActionEvent e) {
            System.out.println("Notified");
            synchronized(a.this){
             a.this.notify();
            }}
    });
   this.getContentPane().add(notify);
 }
class B implements Runnable
{
    int i=0;
   @Override
    public void run() {
        while(i<10){
            System.out.println(" i = "+i);
           // i++;
        }
    }
}
}

当点击等待按钮时,bthread 是否有可能进入等待状态?

【问题讨论】:

    标签: java multithreading wait


    【解决方案1】:

    你想让bthread 真正暂停它的执行,不管它在做什么?没有办法做到这一点,AFAIK。但是,您可以在某些共享状态同步对象上设置bthread 轮询(例如CountDownLatchSemaphore,查看java.util.concurrent 包),以便您更改对象的状态以设置bthread等待。

    【讨论】:

      【解决方案2】:

      没有。你不能这样挂起一个线程。

      但是你可以在B类中实现一个等待方法:

      class B implements Runnable
      {
        private boolean wait = false;
      
        public void pause() {
          wait = true;
        }
      
          int i=0;
         @Override
          public void run() {
              while(i<10){
                  if (wait) {
                     wait();
                  }
                  System.out.println(" i = "+i);
                 // i++;
              }
          }
      }
      

      【讨论】:

        【解决方案3】:

        我不这么认为。 线程 B 可以检查一些变量,例如布尔暂停;如果它是真的,它可以等待。它需要是不稳定的或需要同步,并且需要一些东西来唤醒它,但这取决于你想让它做什么。

        但是如果线程 B 正在做一些长时间的操作,它可能会运行很长时间才检查它是否应该等待。

        【讨论】:

          【解决方案4】:

          不,您只能控制当前线程,如果您在另一个线程上等待,您实际上调用 wait() 使用该对象(您所指的线程)作为监视器。所以你要么必须超时,要么有人在那个对象上调用中断来让你当前的线程重新开始。

          您必须将该逻辑构建到您的程序中,使其在标记变量或消息后等待。另一种方法是使用锁或信号量。

          如果您希望该线程停止,您也可以在该线程上调用中断,但该逻辑也必须内置到您的程序中,因为如果线程正在执行 IO,它可能只会抛出 InterruptedException。

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2019-10-31
            • 1970-01-01
            • 1970-01-01
            • 2021-08-22
            • 2019-06-07
            • 1970-01-01
            相关资源
            最近更新 更多