T
T
tincap2015-12-30 16:43:18
Java
tincap, 2015-12-30 16:43:18

How to restart one thread in console?

There is a java program that I run through the console. It creates 4 threads, and each thread prints its data to the console. At the output, I see a bunch of lines from all 4 threads. Sometimes it may happen that something happens in one of the threads that requires it to be restarted. Are there any tools for dealing with multiple threads? Because if I press Ctrl + C, then all threads will stop.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
N
nirvimel, 2015-12-30
@tincap

What tools do you mean? Outside it is impossible to influence the course of the program. Exception handling must be provided in the code. Usually for this they implement something like a scheduler (scheduler) in a separate thread, which regularly polls the worker threads (worker) and if the worker did not have time to respond to the request within a certain time, it is killed and restarted. Communication between threads goes through ConcurrentLinkedQueue queues (sometimes BlockingQueue, but usually they are used not for polling, but for sending data streams). The worker polls the incoming queue in a loop and responds to the ping through the outgoing one. It is also a good idea to wrap the entire worker in a RuntimeException handler, in which to signal the scheduler about what has happened.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question