Position:Home>JAVA Development> The blemish of Java line Cheng
The blemish of Java line Cheng
From;  Author:Stand originally





TimeoutException is RuntimeException derive kind, it is cast to go out namely after awaiting overtime.

Be need overtime, but still can't make code strong. You still need to have the capacity that awaits from lock of request of exterior break down. So, convey when the line Cheng that awaits a lock to after an Interrupt() method, this method should toss an object of a SynchronizationException, interrupt the line Cheng that await. A derive that this should be RuntimeException unusually kind, need not not handle it especially so.

The changes a method main problem that these syntactic to Synchronized commendation is, they need to be revised on binary code class. And at present these code are used enter monitoring (Enter-monitor) and exit monitoring (Exit-monitor) instruction will realize Synchronized. And these instructions do not have parameter, need the definition of code of patulous binary system to decide a request in order to support many locks so. But this kind is revised won't more relaxed than be in what Java fictitious aircraft modifies in Java 2, but it is to be down compatible and existent Java code.

Another resoluble problem is the commonnest dead lock circumstance, below this kind of circumstance, two lines Cheng is awaiting the other side to complete a certain operation. An example below the tentative idea (of hypothesis) :

Imagine Cheng of a line to call A() , but be stripped to run power before obtaining Lock2 after obtaining Lock1. Cheng of the 2nd line is entered move, call B() , obtained Lock2, but take up as a result of Cheng of the first line Lock1, it is so unavailable Lock1, so it is in subsequently await condition. Right now Cheng of the first line is waked up, it tries to obtain Lock2, but because be held by Cheng of the 2nd line, so unavailable. Appear to be locked up to death right now. The grammar of the Synchronize-on-multiple-objects below can solve this problem:

Compile implement (or fictitious machine) the order that can arrange a request to lock up afresh, make Lock1 always is obtained above all, this eliminated dead lock.

But, this kind of method is not regular to multi-line Cheng assembly work, so provide a few meanses break dead lock automatically. A simple way is awaiting the 2nd lock to release acquired to lock up constantly namely. This that is to say, should adopt as follows await means, is not to await forever:

If await every program of the lock to use different overtime value, can break dead lock and among them Cheng of a line can move. I suggest to replace the code in front with the following grammar:
Synchronized statement will await forever, but the regular meeting when it abandons acquired lock dying potentially in order to break the lock is likely. Below good case, every repeat the overtime value that await to differ to be worth randomly than before one.

Improve Wait() and Notify()
Wait()/ Notify() system also has a few problems: Cannot detect Wait() is normal return or return because of overtime. Cannot use traditional condition variable to come true be in condition of ”(signaled) of signal of a “ . The monitoring of too incidental nest (Monitor) lock is decided.
Previous 1 2 3 4 56 7 8 9 10 11 12 13 Next