Java 7.x 中的 validateTree 不起作用(在 Java 6.x 中很好)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9758069/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
validateTree in Java 7.x doesnt work (in Java 6.x was fine)
提问by user1276830
My version of java is:
我的java版本是:
Java Plug-in 10.3.1.255 Using JRE version 1.7.0_03-b05 Java HotSpot(TM) Client VM
Java 插件 10.3.1.255 使用 JRE 版本 1.7.0_03-b05 Java HotSpot(TM) 客户端 VM
So when I had version 6.x everything was fine, after upgrading I've got this:
所以当我有 6.x 版时一切都很好,升级后我得到了这个:
Exception in thread "AWT-EventQueue-0" java.lang.IllegalStateException: This function should be called while holding treeLock at java.awt.Component.checkTreeLock(Component.java:1196) at java.awt.Container.validateTree(Container.java:1682) at pl.recorder.ScenarioWindow.showUploadPanel(PlayerWindow.java:721) at pl.recorder.actions.UploadFilesAction.execute(DesignFilesAction.java:71) at pl.recorder.actions.EndTestAction.actionPerformed(EndTestAction.java:91) at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2018) at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2341) at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402) at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259) at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:252) at java.awt.Component.processMouseEvent(Component.java:6505) at javax.swing.JComponent.processMouseEvent(JComponent.java:3321) at java.awt.Component.processEvent(Component.java:6270) at java.awt.Container.processEvent(Container.java:2229) at java.awt.Component.dispatchEventImpl(Component.java:4861) at java.awt.Container.dispatchEventImpl(Container.java:2287) at java.awt.Component.dispatchEvent(Component.java:4687) at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4832) at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4492) at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4422) at java.awt.Container.dispatchEventImpl(Container.java:2273) at java.awt.Window.dispatchEventImpl(Window.java:2713) at java.awt.Component.dispatchEvent(Component.java:4687) at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:707) at java.awt.EventQueue.access0(EventQueue.java:101) at java.awt.EventQueue.run(EventQueue.java:666) at java.awt.EventQueue.run(EventQueue.java:664) at java.security.AccessController.doPrivileged(Native Method) at java.security.ProtectionDomain.doIntersectionPrivilege(ProtectionDomain.java:76) at java.security.ProtectionDomain.doIntersectionPrivilege(ProtectionDomain.java:87) at java.awt.EventQueue.run(EventQueue.java:680) at java.awt.EventQueue.run(EventQueue.java:678) at java.security.AccessController.doPrivileged(Native Method) at java.security.ProtectionDomain.doIntersectionPrivilege(ProtectionDomain.java:76) at java.awt.EventQueue.dispatchEvent(EventQueue.java:677) at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:211) at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:128) at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:117) at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:113) at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:105) at java.awt.EventDispatchThread.run(EventDispatchThread.java:90)System.getProperty("java.version");
I changed validateTree()
to validate()
and suprise ;) - everthing works fine, but I dont know if this change will work also on java 6.x (I guess not). How I can change this code to work in Java 6x and 7x.
我改validateTree()
到validate()
和惊喜;) -寄托都工作正常,但我不知道这是否改变也将工作于Java 6.x的(我想不会)。我如何更改此代码以在 Java 6x 和 7x 中工作。
回答by Perry Monschau
I'm not entirely sure, but I think you can use:
我不完全确定,但我认为您可以使用:
synchronized(getTreeLock()) {
validateTree();
}
Just check if it's 6 or 7 and do something different depending on that.
只需检查它是 6 还是 7 并根据它做一些不同的事情。
回答by vince
"It's not a bug, it's a feature" of java 7 ;)
“这不是错误,而是 Java 7 的一个特性”;)
This function should be called while holding treeLock
这个函数应该在持有 treeLock 时调用
This is to force you writing :
这是强迫你写:
##代码##回答by Stephen C
In response to @vince's Answer, I think it is instructive to look at what the Java 1.4.2 javadocfor the method says:
针对@vince 的回答,我认为查看Java 1.4.2 javadoc中该方法的内容是有益的:
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
.
protected void validateTree()
递归下降容器树并重新计算任何标记为需要它的子树(标记为无效的子树)的布局。同步应由调用此方法的方法提供:
validate
。
(Emphasis added.)
(加了重点。)
The way I read this, it is saying that the method is designedto be called by validate()
which will (presumably) be holding the tree lock.
我读这个的方式是说该方法被设计为由validate()
它(大概)持有树锁来调用。
Note that the text is identical in Java 6 and Java 7. The spec has not changed ...
请注意,Java 6 和 Java 7 中的文本是相同的。规范没有改变......
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.
现在显然有应用程序代码validateTree()
直接调用......而无需获取树锁。据推测,当最终用户点击太快或其他东西时,这会导致无法重现的问题(Heisenbugs)。据推测,Java 7 中的更改旨在引起validateTree()
开发人员注意这种不正确/错误的使用。
OK, so this is short-term pain. But the long term, everyonewins (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.
Oracle 不会收到关于奇怪的不可重现行为的错误报告,而这些行为实际上是应用程序程序员的问题。
开发人员不会从客户那里收到关于奇怪的不可重现行为的错误报告。
最终用户获得运行更好的应用程序。