abc
abc

Reputation: 315

wait call for a particular thread

Is it possible to call wait method for another thread rather than the current thread.what I am 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 wait button is clicked the bthread go into wait state?

Upvotes: 3

Views: 376

Answers (4)

michele b
michele b

Reputation: 1865

do you want bthread to actually pause its execution, whatever it is doing? There's no way to do that, AFAIK. You may however set bthread polling on some shared stateful synchronization object (a CountDownLatch or a Semaphore for instance, look into the java.util.concurrent package), so that you alter the status of the object to set bthread waiting.

Upvotes: 6

polve
polve

Reputation: 3158

No, you can only control the current thread, if you wait on another thread 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 call interrupt on that object to make your current thread start again.

You have to build that logic into your program, causing it to wait after a variable or message be flagged. Another way would be using locks or semaphores.

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

Upvotes: 1

barsju
barsju

Reputation: 4446

No. You can't suspend a thread like that.

But you can implement a wait method in the B class:

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

Upvotes: 3

Alpedar
Alpedar

Reputation: 1344

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

But if thread B is doing some long operation, it can be running long time before it checks whether it should wait.

Upvotes: 1

Related Questions