java - restart a thread after exception is caught -


I am reading a UDP feed and I want to try to restart the thread if its failure. I think I have written my class correctly, but someone has to see and see if something is wrong or I have written something that will not work. In my catch-up section, I try to restart every 6 second thread for 10 attempts. Is this a good solution, will it work?

  class UDPReader thread {Private thread t; Private finals string ip, socket, queue, threads; Private string error message; Private Final JTextArea Screen; UDPReader (string IP, string socket, string queue, string thread name, JTextArea screen) {this.ip = ip; this. Socket = socket; This.queue = queue; This.threadName = threadName; This.screen = Screen; } Public Zero Run () {try {byte [i] = null; IpaddrConnection ipaddr = new ipaddrConnection (IP, Socket); ParseUDP P = new parseUDP (); Screen.append ("Thread" + Thread name + "Running \ n"); While (true) {i = ipaddr.getPacket (); P.parseUDP (i); //Thread.sleep(0); }} Hold (IOException pre) {Logger.getLogger (MarketDataReader.class.getName ()). Log (Level.SEVERE, null, ex); Error message = "thread" + thread name + "has failed, attempt to restart"; Screen .append ("thread" + name of thread + "has failed, trying to restart \ n"); Email Email = New Email (Error Message, "Warning Market Data Reader Failure"); For (int i = 0; i & lt; 10 & amp; amp; t.isAlive () == wrong; i ++) {try {start (); Thread.Sleep (6000); } Grip (Interrupted Precision EX1) {Logger.getLogger (UDPReader.class.getName ()). Logs (level SESEE, blank, EX1); Error message = "thread" + thread name + "has failed, contact system system"; Screen.append ("Thread" + name of thread + "has failed, contact system system \ n"); Email = new email (error message, "warning market data reader failure"); }}}} Public Zero Start () {If (T == empty) {t = New thread (this, thread name); T. Start (); }}}}  

I do not know the reasoning behind my thread, but I make some suggestions on the design of my code.

  1. It is not clear that inside the class you get from thread, you have another thread.

  2. There is no need to get from the thread (and I believe this is usually a bad practice). It is common to implement the runable interface and to create a new thread using it.

      Class UDPReader implements Runnable {...}  

    and after that one Instant Thread:

      Thread T = New Thread (New UDPReader ());  
    1. As a rule, if a thread fails, it ends ... It does not look like a failed thread "Re" better solution is a thread. UncaughtExceptionHandler will provide and analyze your thread's failing status and you can restart the thread if necessary.

    In general, in concurrent programming, you must explicitly separate the logic of a thread and the external argument, which is the management of this thread (its beginning / interruption / termination). ). I think, this is what you have not done in your code.


Comments

Popular posts from this blog

java - Can't add JTree to JPanel of a JInternalFrame -

javascript - data.match(var) not working it seems -

javascript - How can I pause a jQuery .each() loop, while waiting for user input? -