Call waiting for a specific thread

Is it possible to call the wait method on a thread other than the current thread. What I'm asking is something like this:

code:

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++;
        }
    }
}
}

      

Is it possible that when the wait button is pressed, bthread

it enters the wait state?

+3


source to share


4 answers


Do you want to bthread

actually pause its execution, no matter what it does? There is no way to do this, AFAIK. However, you can set the poll bthread

to some general state synchronization object (a CountDownLatch or Semaphore , look at the java.util.concurrent package to change the state of the object to set a bthread

wait.



+6


source


Not. You cannot pause such a thread.

But you can implement a wait method in class 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


source


I do not think so. Thread B can check some variable, for example boolean pause; If this is true, it can wait. It has to be choppy or needs synchronization, and something to wake it up is necessary, but that depends on what you want.

But if thread B is doing a long operation, it can run for a long time before checking to see if it should wait.

+1


source


No, you can only control the current thread if you are waiting for another thread, which you actually call wait () using that object (the thread you are referring to) as a monitor. So you either have to time out or someone calls an interrupt on that object to resume the current thread.

You must build this logic in your program by making it wait after a variable or message has been marked. Another way is to use locks or semaphores.

You can also call an interrupt on that thread if you want it to stop, but this logic must be built into your program as well, as this might just throw an InterruptedException if the thread is doing IO.

+1


source







All Articles