Howdy, Stranger!

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

Sign In with Facebook Sign In with Google Sign In with OpenID

Categories

We have migrated to a new platform! Please note that you will need to reset your password to log in (your credentials are still in-tact though). Please contact lee@programmersheaven.com if you have questions.
Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

pause and resume a thread

saran185saran185 Posts: 23Member
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

  • nbfelicianbfelicia Posts: 3Member
    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.