Hi all
Firstly if you need more details, feel free to ask me and I will add them.
I have a problem with two threads dying when there both in a while true statement.
The Main class creates two threads (Start and Stop).
These threads are in a while true as I need them to run forever.
They check times in a database for starting and stopping other processes
When a start date is found, the thread creates another thread to start the process and continues checking for another date.
The stop threads works in the same way.
Iv posted my start thread below.
Any help or ideas will be useful
package startstopthreads; import java.sql.ResultSet; import java.sql.SQLException; import java.text.DateFormat; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.Date; public class StartThread extends Thread { DbConnection Db; ResultSet Rs1 = null; Date EventStartDate, EventStopDate, EventSyncDate; DateFormat DateFormat = new SimpleDateFormat(GlobalDataStore.DateTimeFormat); LoggingToFile MessageLog; Date Now = null; int Flag = 0; /** * Constructor */ public StartThread() { //Creates an instance of the log file for errors / changes MessageLog = new LoggingToFile(); } /** * This method is ran by the Main Thread */ @Override public void run() { Db = new DbConnection(); // While true means that this constantly runs while (true) { // Set up database connection and Gets all the events that are Waiting Db.CreateConnection(); Rs1 = Db.GetEventNames(Flag); // Get current time Now = new Date(); try { while (Rs1.next()) { Event event = new Event(Rs1.getInt("Event_ID")); event.setStartDate(Rs1.getString("Event_StartDate")); event.setThreadFlag(Rs1.getInt("Event_Thread_Flag")); event.setSyncID(Rs1.getInt("Event_Sync_ID")); //If the start date is before now, do all the checks EventStartDate = StringToDate(event.getStartDate()); if (EventStartDate != null) { if (EventStartDate.before(Now) && event.getThreadFlag() == 0) { //If there is a sync event, edit the stop time if (event.getSyncID() != 0) { event.setSyncDate(Rs1.getString("Sync_StopDate")); EventSyncDate = StringToDate(event.getSyncDate()); if (EventSyncDate.after(Now)) { System.out.println("Found Sync Event"); //Change the stop time to equal the synchronisation ChangeStopTimeAccordingToSync(event); //Set up the event object and playout the stream event.setEventId(Rs1.getInt("Event_ID")); event.setServerId(Rs1.getInt("PS_ID")); event.setHostName(Rs1.getString("PS_Host_Name")); event.setPortId(Rs1.getInt("Event_Ports_ID")); event.setPortNo(Rs1.getInt("Ports_Number")); event.setStopDate(Rs1.getString("Event_StopDate")); event.setInBitrate(Rs1.getInt("Event_InBitrate")); event.setOutBitrate(Rs1.getInt("Ports_Out_Bitrate")); event.setStreamName(Rs1.getString("Event_Stream_Name")); event.setLoops(Rs1.getInt("Event_Loops")); event.setEventName(Rs1.getString("Event_Name")); event.setType(Rs1.getInt("Stream_Dektek_Type")); event.setAllowedPids(Rs1.getString("Event_AllowedPIDs")); event.setExcludePids(Rs1.getString("Event_ExcludePIDs")); event.setMapped(Rs1.getString("Event_PIDsMapped")); PlayStreamOut(event); } } else { //If theres no sync event, just play the stream out event.setStopDate(Rs1.getString("Event_StopDate")); EventStopDate = StringToDate(event.getStopDate()); if (EventStopDate.after(Now)) { System.out.println("Found Event"); //Set up the event object and playout the stream event.setEventId(Rs1.getInt("Event_ID")); event.setServerId(Rs1.getInt("PS_ID")); event.setHostName(Rs1.getString("PS_Host_Name")); event.setPortId(Rs1.getInt("Event_Ports_ID")); event.setPortNo(Rs1.getInt("Ports_Number")); event.setStopDate(Rs1.getString("Event_StopDate")); event.setInBitrate(Rs1.getInt("Event_InBitrate")); event.setOutBitrate(Rs1.getInt("Ports_Out_Bitrate")); event.setStreamName(Rs1.getString("Event_Stream_Name")); event.setLoops(Rs1.getInt("Event_Loops")); event.setEventName(Rs1.getString("Event_Name")); event.setType(Rs1.getInt("Stream_Dektek_Type")); event.setAllowedPids(Rs1.getString("Event_AllowedPIDs")); event.setExcludePids(Rs1.getString("Event_ExcludePIDs")); event.setMapped(Rs1.getString("Event_PIDsMapped")); PlayStreamOut(event); } } } } } //Close the connection and sleep the thread for half a second. Db.Close(); this.sleep(500); this.yield(); } catch (SQLException ex) { System.out.println("Exception in getting event names - " + ex); MessageLog.write("Exception in StartThread class. Method - Run (Thread). SQLException - " + ex); } catch (InterruptedException ex) { System.out.println("Exception in sleeping Start Thread - " + ex); } } } /** * Gets the length of the Sync event and sets the stop time to equal now + length * @param ev - the event object passed in */ private void ChangeStopTimeAccordingToSync(Event ev) { //Get the length of the Sync event so we can change the stop time int Length = Db.GetSyncLength(ev.getSyncID()); //Gets whether the length is in hours, minutes or seconds int TimeIn = 0; switch (Db.GetSyncFlag(ev.getSyncID())) { case 1: TimeIn = Calendar.HOUR; break; case 2: TimeIn = Calendar.MINUTE; break; case 3: TimeIn = Calendar.SECOND; break; } //Create calendar object and add the length onto now Calendar NewDate = Calendar.getInstance(); NewDate.add(TimeIn, Length); //Change stop time to equal Now plus 'length' in the future Db.StopEvent(ev.getEventId(), DateFormat.format(NewDate.getTime())); } /** * Converts the string passed in to a date object and then to a calendar object * @param s - the string to convert * @return A calendar object containing the date of the string */ private Date StringToDate(String s) { Date date = null; try { //try parsing once date = DateFormat.parse(s); } catch (ParseException ex) { try { //if fail, try again with seconds date = DateFormat.parse(s + ":00"); } catch (ParseException ex2) { System.out.println("Exception in parsing string to Date in Start Thread - " + ex); MessageLog.write("Exception in StartThread class. Method - StringToDate(Thread). ParseException - " + ex); } } return date; } /** * Checks the Stream type is not 0, if so set this to a normal transport stream ( type 1 ) * @param evnt - the event object of which to create a thread to playout */ private void PlayStreamOut(Event evnt) { //If stream type == null, set type to 1 if (evnt.getType() == 0) { evnt.setType(1); } //Sets the thread flag to 1 so only one flag is created Db.SetThreadFlag(evnt.getEventId(), 1); //Starts the thread and passes the event object to it StartStreamThread Start = new StartStreamThread(evnt); Thread StartStream = new Thread(Start); StartStream.start(); } }
Also to help any readers, the Event object just holds variables, getters and setters
The MessageLog logs any changes and exceptions
Thanks
Kurt