pause and resume a thread

hi..
I want to pause the execution of a thread and then resume it back.. basically i am developing a media player in java. when the audio plays simultaenously a text file content wil be displyed on screen. now when i pause the audio by clicking pause button the display of text should also be paused . i am using 2 threads. 1 st for playing audio. 2nd for displaying text. i have attached my code .when i run iam able to pause the display of text using interrupt() func. but how can i resume the thread again if i play the audio.kindly help..
the below code is for displaying text from a file...[code]
Runnable r2 = new Runnable()
{
public void run()
{
try
{



for(int j=0;j<r.length;j++)
{ textArea1.append(r[j]); // highliting the words
textArea1.append(" ");
textArea1.setForeground(Color.red);
textArea1.setLineWrap(true);
int t=dur/nbWords;
Thread.currentThread().sleep(t);
dur=dur-t;
nbWords--;

}
}
catch (InterruptedException x)
{
x.printStackTrace();
}
}
};
tr2 = new Thread(r2); // thread 2 for highlight
tr2.start();

[/code]

below code is the pausing event.
it must make the tr2 thread to pause execution and then resume it back.
[code]
if (e instanceof StopByRequestEvent)
{
System.out.println("stop");

synchronized(this)
{
try
{

System.out.println("1");

tr2.interrupt();
System.out.println("2");
} catch (Exception e1) { System.out.println(e1); }


}

return;
}
if (e instanceof StartEvent)
{
System.out.println("resume");

synchronized(this)
{
try {
System.out.println("1");
tr2.notify();
System.out.println("2");
} catch (Exception ex) {

}
}
[/code]

Comments

  • Try to use method sleep() for thread.... It can sleep until you press a button...
    Something like this
    while (some button is not pressed)
    {
    Thread.currentThread().sleep(1000);//sleep for 1000 ms
    }

    Hope it's useful..
Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

In this Discussion