0

Problem statement:--

I have a UI button "Perform"-- which will initiate a big background process in loop in a different thread sequentially. As soon the process is started the button text will be toggled to "Pause" Now on press of "Pause", I need to suspend the thread execution and "Resume" will finally notify to go ahead.

On button press"Perform",thread gets started, and button toggles to "Pause" and if I press Pause,thread is suspended.But I am not able to resume the running thread.If I check the Thread state in resume button press event, I get value=0 which is equivalent to NONe. Please help. Following is my code....

Blockquote

performBButton.addSelectionListener(new SelectionListener() {
        @Override
        public void widgetSelected(SelectionEvent e) {
            Button btnToggle = (Button) e.getSource();
            MyJob myJob = new MyJob();
            if (btnToggle.getText().equalsIgnoreCase("Perform")) {
                btnToggle.setText("Pause");
                myJob.schedule();

            }
            else if (btnToggle.getText().equals("Pause")) {
                buttonToggle.setText("Resume");
                isPaused=!isPaused;
            }
            else if (btnToggle.getText().equals("Resume")) {
                buttonToggle.setText("Pause");
                isPaused=!isPaused;
                synchronized (myJob) {
                    myJob.notify();
                  }
            }
        }
    });

class MYJob extends Job{
   public void run(IProgessMonito monitor) {
       @Override
       public void run() {
           for (int i=0;i<50000;i++) {
               System.out.println("Iteration "+i +isPaused);
                    synchronized (this) {
                        if (isPaused) {                               
                                wait();
                            } 
                        }
                    }
         Thread.sleep(1000);//I have removed try catch while pasting here

    }
}
}
}
Nicks
  • 16,030
  • 8
  • 58
  • 65

1 Answers1

0

changing wait() to myThread.wait() actually solved my problem, by declaring myThread globally. but its not the correct way , any further suggestions will be helpful...

Nicks
  • 16,030
  • 8
  • 58
  • 65