validateTree in Java 7.x doesnt work (in Java 6.x was fine)

百般思念 提交于 2019-11-28 11:19:16
vince

"It's not a bug, it's a feature" of java 7 ;)

This function should be called while holding treeLock

This is to force you writing :

synchronized(getTreeLock()) {
     validateTree();
}

In response to @vince's Answer, I think it is instructive to look at what the Java 1.4.2 javadoc for the method says:

protected void validateTree()

Recursively descends the container tree and recomputes the layout for any subtrees marked as needing it (those marked as invalid). Synchronization should be provided by the method that calls this one: validate.

(Emphasis added.)

The way I read this, it is saying that the method is designed to be called by validate() which will (presumably) be holding the tree lock.

Note that the text is identical in Java 6 and Java 7. The spec has not changed ...

Now apparently there is application code out there that is calling validateTree() directly ... without acquiring the tree lock. Presumably, this results in unreproducible problems (Heisenbugs) when the end user clicks too fast or something. Presumably, the change in Java 7 is designed to bring this incorrect / buggy use of validateTree() to developers' attention.

OK, so this is short-term pain. But the long term, everyone wins (apart from the lawyers :-) ):

  • Oracle doesn't get bug reports for strange unreproducible behaviour that is really the application programmer's problem.

  • Developers don't get bug reports from customers for strange unreproducible behaviour.

  • End users get applications that work better.

I'm not entirely sure, but I think you can use:

System.getProperty("java.version");

Just check if it's 6 or 7 and do something different depending on that.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!