SageTV Community  

Go Back   SageTV Community > SageTV Development and Customizations > SageTV Studio
Forum Rules FAQs Community Downloads Today's Posts Search

Notices

SageTV Studio Discussion related to the SageTV Studio application produced by SageTV. Questions, issues, problems, suggestions, etc. relating to the Studio software application should be posted here.

Reply
 
Thread Tools Search this Thread Display Modes
  #1  
Old 05-16-2010, 04:39 PM
tmiranda's Avatar
tmiranda tmiranda is offline
SageTVaholic
 
Join Date: Jul 2005
Location: Central Florida, USA
Posts: 5,851
Runnable classes

Before I start banking my head against the wall in trying to figure out how to get a runnable class to work I'm hoping to get answers to some very basic questions. Help greatly appreciated.

Can somebody confirm that the general stun of the code should look like this:

Code:
public class MyClassName implements Runnable {

     public void run () {

     }

}
It seems too easy, so I know I'm missing something Anything special needed in run()?

For my purposes I want the thread to stay alive as long as the sage server is running. Is it safe to create an endless loop (it will sleep for long periods of time) or do I need to put in some type of checks for sage shutting down?

Do I need to do anything special in the IDE (NetBeans) or do I just compile the code like a normal .JAR file?
__________________

Sage Server: 8th gen Intel based system w/32GB RAM running Ubuntu Linux, HDHomeRun Prime with cable card for recording. Runs headless. Accessed via RD when necessary. Four HD-300 Extenders.
Reply With Quote
  #2  
Old 05-16-2010, 05:09 PM
PLUCKYHD PLUCKYHD is offline
SageTVaholic
 
Join Date: Dec 2007
Posts: 6,257
Looks okay you need a stop() as well so it knows what to do when it stops.

I sleep then loop repeat. Sean prefers timers which work as well. I found sleeping works pretty well and it acceptable.

Just make sure and catch your exceptions and do something with them.

Also I prefer to make it a Deamon thread so it gets lower priority and doesn't stop sage from shutting it down if the service is stopped.
Reply With Quote
  #3  
Old 05-16-2010, 05:12 PM
stuckless's Avatar
stuckless stuckless is offline
SageTVaholic
 
Join Date: Oct 2007
Location: London, Ontario, Canada
Posts: 9,713
Tom, if you are going to create a thread that basically "polls" every n # of seconds, then I'd suggest you take advantage of the Java Timer and TimerTask classes. These are there to simplify your dealing with threads, etc, for the simple purpose of polling.

Code:
Timer timer = new Timer();
TimerTask mytask = new TimerTask() {
    public void run() {
        // do your work
    }
};
timer.scheduleAtFixedRate(mytask, 0, 60*1000); // repeats every 60 seconds, starts immediatly
EDIT: Looks like I'm getting predictable Plucky is predicting my responses now

Last edited by stuckless; 05-16-2010 at 05:14 PM.
Reply With Quote
  #4  
Old 05-16-2010, 05:36 PM
Slugger Slugger is offline
SageTVaholic
 
Join Date: Mar 2007
Location: Kingston, ON
Posts: 4,008
Don't worry about the stop() method, it'll never be called. That method's long since been deprecated because it can cause deadlock. If you're going to go with the work, sleep, work loop, it should look something like this:

Code:
public void run() {
   while(true) {
      // Do your work
      try {
         Thread.sleep(300000); // Sleep for 5 mins
      } catch(InterruptedException e) {
         e.printStackTrace(); // Log who killed your thread, if you like
         Thread.currentThread().interrupt(); // Make sure outer loops also get the signal (not necessary in this example, but never a bad idea)
         break; // Exit the while loop
      }
   }
}
So your thread will run until someone interrupts (presumably, that'll be the Sage core at Sage shutdown).
__________________
Twitter: @ddb_db
Server: Intel i5-4570 Quad Core, 16GB RAM, 1 x 128GB OS SSD (Win7 Pro x64 SP1), 1 x 2TB media drive
Capture: 2 x Colossus
STB Controller: 1 x USB-UIRT
Software:Java 1.7.0_71; SageTV 7.1.9
Clients: 1 x HD300, 2 x HD200, 1 x SageClient, 1 x PlaceShifter
Plugins: Too many to list now...
Reply With Quote
  #5  
Old 05-16-2010, 05:41 PM
tmiranda's Avatar
tmiranda tmiranda is offline
SageTVaholic
 
Join Date: Jul 2005
Location: Central Florida, USA
Posts: 5,851
I created a basic runnable class that just prints a line to the sage log and, much to my amazement, it worked on the first try. It just goes to show you that even a blind squirrel finds a nut every once in a while.

Plucky - I'm not sure I know what you mean by needing a stop(). When Sage ends I want the runnable class to end. The thread will not have to do anything special. What would normally be done by stop()?

Why does the runnable class stop sage from shutting down? (i.e. Why do I need a daemon thread?)

Thanks for the info on catching exceptions. I haven't done that yet so I guess it's about time I learned how....

Sean - It doesn't really "poll". It sleeps for several hours and then wakes up to do some work, then goes back to sleep. It should do this as long as the sage server is up and running.

The "work" it does is to look for favorite podcasts and then if they need to be downloaded, send a message to my servlet that will download them.

Using timers seems straight forward (famous last words) so I'll go down that path if it's better than looping and sleeping.
__________________

Sage Server: 8th gen Intel based system w/32GB RAM running Ubuntu Linux, HDHomeRun Prime with cable card for recording. Runs headless. Accessed via RD when necessary. Four HD-300 Extenders.
Reply With Quote
  #6  
Old 05-16-2010, 05:45 PM
tmiranda's Avatar
tmiranda tmiranda is offline
SageTVaholic
 
Join Date: Jul 2005
Location: Central Florida, USA
Posts: 5,851
Slugger - Thanks for the info. I think we were both typing at the same time.
__________________

Sage Server: 8th gen Intel based system w/32GB RAM running Ubuntu Linux, HDHomeRun Prime with cable card for recording. Runs headless. Accessed via RD when necessary. Four HD-300 Extenders.
Reply With Quote
  #7  
Old 05-16-2010, 06:33 PM
stuckless's Avatar
stuckless stuckless is offline
SageTVaholic
 
Join Date: Oct 2007
Location: London, Ontario, Canada
Posts: 9,713
Quote:
Originally Posted by tmiranda View Post
Using timers seems straight forward (famous last words) so I'll go down that path if it's better than looping and sleeping.
Basically the timers do the looping and sleeping for you
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Any experience in Sun online classes? PLUCKYHD SageTV Studio 5 03-23-2010 03:20 AM
Easy way to dynamically build classes/objects PLUCKYHD SageTV Studio 27 02-25-2010 06:54 AM
Calling custom java classes from studio davin SageTV Studio 9 11-09-2005 11:50 AM


All times are GMT -6. The time now is 06:08 PM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2023, vBulletin Solutions Inc.
Copyright 2003-2005 SageTV, LLC. All rights reserved.