How to ensure that a code block cannot be interrupted by any other thread in Java
•
Java
For example:
new Thread(new Runnable() { public void run() { while(condition) { *code that must not be interrupted* *some more code* } } }).start(); SomeOtherThread.start(); YetAntherThread.start();
How do you ensure that code that does not break does not break?
Solution
You can't - at least you can't use normal Java and run on a normal non real time operating system Even if other threads do not interrupt you, other processes may do so Basically, you can't guarantee that you'll get a CPU before you finish the operation If you want such a guarantee, you should use Java real-time system I don't know if I know it will certainly provide the facilities you want
It is best to avoid this requirement first
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
二维码