0

I have made two thread running together with Netbeans and i want to add three buttons for stop-pause-continue them.They highlight and de-highlight a text.The code for one of them is

class TimeHighspell extends Thread{
    int x=0;
    int pos;
    int delay;
    Control control ;
    String [] result;
    int[] anArray;
    public TimeHighspell (Control c, int delay,String [] result1,int pos,int[] anArray) {
        x=0;
        control = c;
        this.delay = delay;
        result=result1;
        this.pos=pos;
        this.anArray=anArray;
    }
public void run(){
    while (true) {
        control.putHighspell(result,pos,x);
        int Search=x+1;
        Arrays.sort(anArray);
        int index = Arrays.binarySearch(anArray,Search); 
        if (index > 0) 
            pos=pos+result[x].length()+1;
        else
            pos=pos+result[x].length();
        x=x+1;
        try {
            Thread.sleep( delay );
        }
        catch ( InterruptedException e ) {}
   }}}
class Control {
    private boolean ping = false;
    public synchronized void putHigh(String [] result,int pos,int x){     
       while(ping==false)
            try{
                //notify();
                wait();
            }catch( InterruptedException e){}
        highlight(editPane, result[x],pos);
        HighBold(editPane,result[x],pos);
        ping = false;
        notifyAll();
}

To start them i use a a button where i put this code

 private void HighWordActionPerformed(java.awt.event.ActionEvent evt) {                                         
    String [] result1 = editPane.getText().split("\\s");
    Control c = new Control();
    int delay;
    int pos=0;
    delay=Slider.getValue();
    RemoveHigh p1 = new RemoveHigh(c, delay);
    TimeHigh c1 = new TimeHigh(c, delay,result1,pos);
    p1.start();
    c1.start();}
drew
  • 221
  • 1
  • 5
  • 10

2 Answers2

0

Actually you have many solution, I will try to describe some of those:

you can make 2 static booleans variable in main, and check them in threads.

Also you can make an static String variable, and to check value of that in threads.

OR, you can put an Semaphore in main thread, as a static field, and in others threads you can access the tryAcquire() method.

In main you have this code :

static public Semaphore semaphore = new Semaphore(2);

In thread you check the state of the semaphore :

while(!semaphore.tryAcquire(2)) {
    Thread.sleep(100);
    if(!semaphore.tryAcquire(1)) {
       //here kill the thread
    }
}

So in the pause button you just put :

semaphore.acquire(1); 

In stop button :

semaphore.acquire(2);

In start button :

semaphore.release(2);
savionok
  • 485
  • 4
  • 11
  • When you say in thread you mean in the run method? Instead of while(true) i do the check? – drew Feb 01 '12 at 00:53
0

Finally i used logical flags following and older answer from @Roman How to indefinitely pause a thread in Java and later resume it?

Community
  • 1
  • 1
drew
  • 221
  • 1
  • 5
  • 10