-2

I have a client-server application that runs the receive method to run in a separate thread. Thread is given some time to finish the job and the thread will be checked for the status.

There are occasions when the receive method will be blocked due to packet or ACK loss. If that happens, how can I stop the thread and start it again the next attempt?

As we all know, Thread.stop() is deprecated.

vhu
  • 12,244
  • 11
  • 38
  • 48
Vishal S
  • 73
  • 11
  • 1
    Please add code. Are we talking about TCP or UDP? – Fildor Nov 04 '15 at 07:29
  • 3
    Why would the thread need to be stopped just because something happened in the network communication? – Kayaman Nov 04 '15 at 07:46
  • @Kayman is right. You shouldn't need to restart the thread, just handle it properly. Packetloss is expected. In case of TCP, it'll be handled for you by the TCP stack; you just need to give it some time. – vhu Nov 04 '15 at 08:27
  • 1
    You can try sending the thread an interrupt and handle that in your code. – stalet Nov 04 '15 at 08:42
  • You're thinking about this wrong. You don't want to start or stop a thread, you want to stop doing some particular work and perhaps start doing some other kind of work. Don't think about threads when you frame these kinds of questions -- it leads to very bad implementations. – David Schwartz Nov 04 '15 at 10:04
  • @Kayaman Because i defined some variables that thread would alter which will later be used in the main thread. – Vishal S Nov 04 '15 at 18:50
  • @VishalS That doesn't warrant thread "restarting" either. If you edit your question to include relevant parts of your code, we can help you find a good solution. – Kayaman Nov 04 '15 at 19:13
  • @David Schwartz Thats exactly what i am trying to do. I feel sorry to ask such a stupid question by not considering the fact that thread be safe to be blocked at the receive input as it would eventually be released later on, as a result of getting input from the remote server. Would not you still recommend using threads? – Vishal S Nov 04 '15 at 19:19
  • @Kayaman I will post the link soon yet there is no need. Say i have a while loop running inside the thread and it is waiting on the receive thread (Lets consider the packet is lost). So i wait for a considerable amount of time (Say EstimatedRTT from the previous packet) to allow packet to be received ( but not received in my case). And then i resend the packet, will thread be able to receive that packet this time and update the status so that i make my main thread to send the next packet? . – Vishal S Nov 04 '15 at 19:45
  • I see now that this is a waste of my time. Have fun restarting your threads. – Kayaman Nov 04 '15 at 19:47
  • @Kayaman I have already agreed that i misconceived, there is no need to restart the thread, once it has been started. But now i need answer for the question. Educate someone something you know, will never be the waste of your time. I hope you understand i am amateur to the field and help me with this. – Vishal S Nov 04 '15 at 20:16
  • @VishalS There's nothing to do. The thread is waiting for data to be received, you want the thread to continue waiting for data to be received. So what's the issue? – David Schwartz Nov 04 '15 at 21:19
  • 1
    @VishalS It's hard to educate someone when you only get a vague description. Many questions on SO get closed because they don't have the code (or rather a part of it) in the question. A simple rule here is "the better the question, the better (and faster) the answer". – Kayaman Nov 05 '15 at 06:32
  • Possible duplicate of [How to start/stop/restart a thread in Java?](http://stackoverflow.com/questions/1881714/how-to-start-stop-restart-a-thread-in-java) – Raedwald Jan 23 '16 at 17:40

3 Answers3

2

You can't restart a Java thread at all, with or without Thread.stop().

You have to create a new one.

You can however reuse a Runnable.

user207421
  • 305,947
  • 44
  • 307
  • 483
0

You can use interrupts to send to the thread and handle them to do a retry. Here is a sample that will start a thread that will not quit until the boolean done is set. However i'm interrupting the thread from a main thread to make it start over.

public class Runner implements Runnable {

    private boolean done;

    @Override
    public void run() {
        while (!done) {
            try {
                doSomeLongRunningStuff();

            } catch (InterruptedException e) {
                System.out.println("Interrupted..");
            }
        }
    }

    private void doSomeLongRunningStuff() throws InterruptedException {
        System.out.println("Starting ... ");
        Thread.sleep(300);
        System.out.println("Still going ... ");
        Thread.sleep(300);
        done = true;
        System.out.println("Done");
    }

    public static void main(final String[] args) throws InterruptedException {
        final Thread t = new Thread(new Runner());
        t.start();
        Thread.sleep(500);
        t.interrupt();
        Thread.sleep(500);
        t.interrupt();
    }
}

Whether you can do it this way or not depends on what you are calling. Your framework doing the TCP connection may or may not support interrupting.

stalet
  • 1,345
  • 16
  • 24
  • would this happen only when the thread is running properly without being blocked and hence stopped gracefully?. What if it is blocked or put to sleep in the middle of execution and not reaching while loop ever again?.Will the code still work? – Vishal S Nov 04 '15 at 20:11
  • This will not work under all circumstances - but as you see in the example that interrupting a thread from sleep is not a problem. You should check how your thread is suspended by looking at the code when connecting or using a debugger to see what is actually happening in your case. – stalet Nov 05 '15 at 08:30
0

We should not restart a thread which is not valid , once thread has comepleted its execution.

Manos Nikolaidis
  • 21,608
  • 12
  • 74
  • 82
arjun99
  • 358
  • 1
  • 8
  • Should programmers never reuse threads?. you mean in the sense that its a bad programming practice. – Vishal S Nov 04 '15 at 20:04