• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Liutauras Vilda
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Scott Selikoff
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
  • Frits Walraven
Bartenders:
  • Stephan van Hulst
  • Carey Brown

Thread coding SOS

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am trying to use the thread methods wait(), notify() or notifyAll() to work as pause and resume on a video player which is run in a thread.

At the moment I can start a thread but when I try and use pause I get an exception from the AWTThread. This is not the thread which my video player is running in.

The application makes use of the Java Media Framework (http://java.sun.com/products/java-media/jmf/). This provides the video player functionality.

The code I am currently using is :

(viewer.java)



[HENRY: Added Code Tags]
[ November 22, 2006: Message edited by: Henry Wong ]
 
author
Posts: 23958
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by ken bennett:
I am trying to use the thread methods wait(), notify() or notifyAll() to work as pause and resume on a video player which is run in a thread.

At the moment I can start a thread but when I try and use pause I get an exception from the AWTThread. This is not the thread which my video player is running in.

The application makes use of the Java Media Framework (http://java.sun.com/products/java-media/jmf/). This provides the video player functionality.



First, you can't call the wait() or notify() method, unless you own the synchronization lock for the notification object. Second, don't call the wait() method from an event handler. Event handlers are supposed to be short -- running code that takes time (such as a wait) will cause you GUI to hang.

Henry
 
ken bennett
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you I will go back to the drawing board. I will look at your Java Threads book. I think that I am going to need to make another thread for the actual player and then synchronized methods for pause and resume.
 
Ranch Hand
Posts: 694
Mac OS X Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What do you think of using t.sleep() instead of wait and then let the thread wake up periodically and check an instance variable:

boolean paused;

If paused is true, then you can call t.sleep() again, otherwise leave the loop that calls sleep() and continue playing the video.

The resume button would then just set the flag boolean paused = false.

-- Kaydell
 
ken bennett
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The way it had to be implemented was to use wait() for the pause and notify() or notifyAll() as resume.

I think that they did not want us to use sleep() because it is now deprecated. I have spent hours trying to do it but I am still no closer to acheiving the desired result. I have been looking for thread examples but usually they are just banking examples.
 
Kaydell Leavitt
Ranch Hand
Posts: 694
Mac OS X Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I believe that Thread.sleep() has not been deprecated. I just looked it up on the 5.0API on Suns web-site. I did read in Core Java, Volume II that notify() is not recommended, but instead, use notifyAll(), but Henry, who has written a book on Threads, said that wait(), notifyAll() are not the correct calls. He said that to use wait(), you have to have the lock on an object.

I'm not sure when wait() / notifyAll() are good. I'll have to read-up some more.

-- Kaydell
 
Henry Wong
author
Posts: 23958
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by ken bennett:
The way it had to be implemented was to use wait() for the pause and notify() or notifyAll() as resume.



Why does it have to use wait and notify? Is it because it is for homework?

Anyway, to use wait and notify, you'll need to modify your player thread to wait (after checking a flag, of course). The event handler can just set and clear the flag as needed and call notify() to ensure that the player wakes up to manage it. Keep in mind, you still need to take care of the synchronization lock (needed for the wait and notify) along with taking care of the race conditions in setting/clearing the flag.

As for using sleep() instead -- I guess that will work too. But without the notify() method, your algorithm (with sleep()) will be a polling algorithm.

Wish I can be of more assistance, but I know little about using the media player.

Henry
 
Henry Wong
author
Posts: 23958
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

I did read in Core Java, Volume II that notify() is not recommended, but instead, use notifyAll(),



<RANT>
This is a Rant. Please feel free to ignore.

NotifyAll() is to be used when a thread has changed a condition that can satisfy more than one thread... hence, you have to wake up more than one thread. There are very few cases where this is needed, as most threaded designs are for thread safety of a common (single) resource. In fact, in most cases, they are only needed during shutdown, where all the threads needs to be awaken so that they may exit.

Unfortunately... most people use notifyAll() because...

- Having threads waiting for different conditions waiting on the same notification object. You have to wake them all because you can't target the correct thread.

- Having incorrect synchronization which cause wait/notify not to work correctly. Using notifyAll partly makes it work better, but not completely.

- Just poor (or no) notification design. Just call notifyAll() to tell all threads something happened, and pray that it works.

Prior to Java 5, only the first case is acceptable -- as it was not possible to have two conditions share the same synchronization lock. This has been fixed with the Lock/Condition class.


Remember, waking up *all* waiting threads, when one thread is needed. Or worse, waking up *all* waiting threads, because there is a bug somewhere in your code, is just plain silly. Fix your design. Or fix your code. Recommending notifyAll() over notify() cause it works better is just wrong.
</RANT>

Sorry about that. I will take my meds now...
Henry
 
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Recommending notifyAll() over notify() cause it works better is just wrong.



Wow, thanks! I don't know how many examples I've seen with notifyAll and I couldn't figure out why this was good ... but I copied it now & then.
 
He baked a muffin that stole my car! And this tiny ad:
New web page for Paul's Rocket Mass Heaters movies
https://coderanch.com/t/785239/web-page-Paul-Rocket-Mass
reply
    Bookmark Topic Watch Topic
  • New Topic