This discussion is archived
2 Replies Latest reply: Mar 2, 2013 2:32 AM by 819351 RSS

I cannot  Stop  a Thread

819351 Newbie
Currently Being Moderated
The goal of the program listed below is to read badge numbers from a badge reader, and add theses numbers into a Vector. Of course the Vector will be processed later.
However, if after 10 seconds nothing has been read, I want to "kill" the thread.
Everything works fine, numbers are well put in the Vector as soon as they are read. When the program runs, and when a timeout occurs I get correcly the two messages : "Timeout Detected" and "Program terminated", but infortunatly the program does not terminate.

I suppose que the t.interrupt() instruction has no effect and this thread remains active. The Class SerialReader created by :
sr = new SerialReader(in);
has a few instructions to listen to the badge reader. When a badge number is read, this value is put into the vector and the run method loops to listen for the next badge.

I guess (but I'm not sure) that even if a t.interrupt() is issued, the SerialReader remains listening and therefore the run method does not stop.

Is there a (clean) way to make this thread stop ? I thought to move " serialPort.close();" before t.interrupt() but I'm not sure it would be ccorect and efficient.

Thanks in advance for any suggestion or comment.

Gerard

PS : I hope this text is understandable because English is not my native language.
import gnu.io.CommPort;
import gnu.io.CommPortIdentifier;
import gnu.io.SerialPort;

import java.io.InputStream;

     public class LectureBadges implements Runnable
     {
          InputStream in= null;
          java.util.Vector v_badges = null;
          protected  int maxBadges = 0;
          // static boolean threadStop = false;
         public LectureBadges(i throws Exception
         {
             super();
             v_badges  =  new java.util.Vector();
             connect("COM1");
             return;
         }
        
         void connect ( String portName ) throws Exception
         {
             CommPortIdentifier portIdentifier = CommPortIdentifier.getPortIdentifier(portName);
             if ( portIdentifier.isCurrentlyOwned() )
             {
                 System.out.println("Error: Port is currently in use");
                 return;
             }
              
                  CommPort commPort = portIdentifier.open(this.getClass().getName(),2000);
                 
                  SerialPort serialPort = (SerialPort) commPort;
                  serialPort.setSerialPortParams(9600,SerialPort.DATABITS_8,SerialPort.STOPBITS_1,SerialPort.PARITY_NONE);
                
                  in = serialPort.getInputStream();
              
                  startProcess();
                  System.out.println("Vector size = " + v_badges.size());
                 System.out.println("Program terminated");
                 serialPort.close();
         }  
         public  void startProcess() 
         {
              try 
              {
                   Thread t = new Thread(this);
                   int oldMax = 0;
                   System.out.println("Reading badges started.    Waiting.......");
                   t.start();
                   while (true) 
                   { 
                        t.join(10000);
                        if (maxBadges <= oldMax)
                        {
                             if (t.isAlive()) t.interrupt();
                             System.out.println("A Timeout had occurred. ");
                             break;
                        }
                        oldMax=maxBadges;
                   }
                   t=null;
              }
              catch (Exception ex ){}
         }      
         public void run ()
         {
              try 
              {
                   while (true)
                 {
                       SerialReader sr = new SerialReader(in);
                       v_badges.add(maxBadges++,sr.badge);
                 }
              }
              catch (Exception ex){System.out.println(ex.toString());}
         } 
         
         public static void main ( String[] args )
         {
             try
             {
                  new LectureBadges() ;
                  System.exit(0);
                  
             }
             catch ( Exception e )
             {
                 e.printStackTrace();
             }
         }
}

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points