multithreading - Who is calling the Java Thread interrupt() method if I'm not? -


I have read Java interaction in practice and have read it again, I have read several formulas on this subject, I have an IBM article And yet there is something that I feel that can be broken into two questions:

  1. If I do not ever disturb other threads in myself, Interrupted exponent ?

  2. If I have never used the other thread in my interrupt () because I am using other methods, Like to cancel my thread, such as poison pills and while (cancel!) style loop [both are explained in jsiip]), an interrupted option So mean? What should I do if I catch one? Close my app?

thread interrupt mechanism (cooperative) thread to prevent a request Asks for what it is doing. at the thread.

In practice, general use-case for interrupt () to some kind of structure or manager to tell some worker thread what to do to prevent it. If the worker thread is "aware block", then it will notice that it has been interrupted by the exception or is checking the interrupted flag from time to time. Keeping in mind that it has been interrupted, a well behaved thread will leave it and the one who is doing it will leave it.

Considering the above-use case, your code is likely to be interrupted by the Java framework or some worker thread. And when it gets interrupted, then your code should be given what it is doing and the reason for ending the most appropriate means. Depending on how your code was called, it can be done by returning or throwing some useful exceptions. But perhaps it should not call system.exit () . (Your application does not necessarily know why it was interrupted, and it certainly does not know whether there are other threads which need to be interrupted by the framework.)

On the other hand, if your code is not Some structures are designed to run in the control, you can argue that the interruption exception is an unexpected exception; That is, a bug in that case, you should treat the exception because you will have other bugs; Like wrap it in an arbitrary exception, and hold and log it at the same point that you deal with other unexpected arbitrary exceptions. (Alternatively, your application can only ignore the interrupt and continue whatever it was doing.)


1) If I ever used the other threads I do not interrupt myself, so what is a blocking exception?

An example is that if your runnable objects use executable service and shutdownNow () Is executed by the service called and in theory, any third-party thread pool or thread management framework can do such a legitimate way.

2) If I am not interfering with other threads then I am using interrupt () ... a constrained events ? What should I do if I catch one? Turn off your app?

You need to find out the codebase whether is interrupt () is calling and why once you have thought that what you do Can >> your & lt; & Lt; Need to share the app.

As long as you do not know why the interrupted option is being thrown , I would advise to treat it as a difficult error; Just print a stacktrace in the log file and close the app (obviously, this is not always the right answer ... but the point is that this is "a bug", and it has to be brought to the attention of the developer / maintenance.)

3) How do I know that interrupt () ?

There is no good answer to what the call is going on. The best I can suggest is set a breakpoint at Thread.interrupt () and look at the call stack.


Comments

Popular posts from this blog

oracle - The fastest way to check if some records in a database table? -

php - multilevel menu with multilevel array -

jQuery UI: Datepicker month format -