There is a requirement to detect the state of two objects, and the task needs to be real-time. The run method uses the while (flag) loop to end the life cycle of the thread by changing flag = false. The thread normally needs to run for 40 minutes or more. Using the thread pool will cause the core thread pool to run out, and the task will enter the queue, because each thread will run for 40 minutes The execution time of each thread is very long and not fixed, so there must be many threads that cannot respond in time.
I try to use new thread (runnable). Start() instead of using thread pool ThreadPoolExecutor.execute (runnable), but I think that this way, although I will cache runnable in a concurrent HashMap, I always feel that threads are not managed by the thread pool, and there may be exceptions (such as disappearing for no reason).
How to solve this problem, or what better way to replace the writing of while (flag).
Thank you for your advice, thank you!!!!!