由于这是一个关于try/finally子句行为的学术问题,我试图使用一个非常通用的例子.嵌套像这样的try/finally子句有什么危险吗?
openDatabaseConnection();
try {
// Methods unrelated to cursor
// ...
String cursor_id = openCursor();
try {
useCursor(cursor_id);
} finally {
closeCursor(cursor_id);
}
// Methods unrelated to cursor
// ...
} catch (Exception e) {
genericLogError();
} finally {
closeDatabaseConnection();
}
Run Code Online (Sandbox Code Playgroud)
具体来说,我很想知道closeCursor()
以前是否保证会被调用closeDatabaseConnection()
.有没有理由嵌套一个finally子句,应该被认为是不好的做法?
是否有任何工具可以查看JVM堆的不同代的对象统计信息?我正在研究潜在的内存泄漏,需要不同代的一些数据.我的用例是在旧代中拍摄对象的快照,等待,然后拍摄另一个快照进行比较.
JVisualVM提供了有关已创建对象的统计信息,但这对我没有多大帮助,因为我没有看到特定对象是否正在被提升或终身比它应该更快.
Jmap提供不同代的统计信息,但不提供基于对象的统计信息.在那个工具中,我只能看到老一代正在成长......
我正在尝试回答以下编程问题:
在heap.java程序中,该insert()
方法在堆中插入一个新节点,并确保保留堆条件.编写一个toss()
方法,将新节点放在堆数组中,而不尝试维护堆条件.(也许每个新项都可以简单地放在数组的末尾.)然后编写一个restoreHeap()
方法来恢复整个堆中的堆状态.使用toss()
多次,随后通过单个restoreHeap()
比使用更有效的insert()
反复当大量数据都必须在一个时间被插入.有关线索,请参阅heapsort的说明.要测试您的程序,请插入一些项目,再扔一些,然后还原堆.
我已经为折腾函数编写了代码,该函数在最后成功插入节点,并且不会修改堆条件.我遇到了这个restoreHeap
功能的问题而我无法绕过它.我已经包含了以下两个功能.
heap.java的完整代码在这里(包括toss()
和restoreHeap()
)
toss()
- 我基于插入功能
public boolean toss(int key)
{
if(currentSize==maxSize)
return false;
Node newNode = new Node(key);
heapArray[currentSize] = newNode;
currentSize++;
return true;
} // end toss()
Run Code Online (Sandbox Code Playgroud)
restoreHeap()
- 我基于trickleUp函数,我得到一个StackOverflowError.
public void restoreHeap(int index)
{
int parent = (index-1) / 2;
Node bottom = heapArray[index];
while( index > 0 &&
heapArray[parent].getKey() < bottom.getKey() )
{
heapArray[index] = …
Run Code Online (Sandbox Code Playgroud) 目前,我的所有域请求都使用以下URLRewrite规则定向到www:
<rule>
<name>Canonical Redirect</name>
<condition name="host" operator="notequal">^test.com</condition>
<condition name="host" operator="notequal">^$</condition>
<from>^/(.*)</from>
<to type="permanent-redirect" last="true">http://www.test.com/$1</to>
</rule>
Run Code Online (Sandbox Code Playgroud)
我有2个要求:
我相信我可以使用以下代码,但似乎我错过了一些东西.感谢您的帮助!
<rule name="RedirectTo" stopProcessing="true">
<match url="http://child.test.com" />
<action type="Redirect" url="http://test.com/child" />
</rule>
Run Code Online (Sandbox Code Playgroud) 我正在验证我的表单,我可以获得textbox的值,但总是在uiClass变量(Error: java.lang.NullPointerException
)中检索null值:有什么建议吗?
例外:
WARNING: /jsf/report/Edit.xhtml @39,99 listener="#{studentController.validate}": java.lang.NullPointerException
javax.el.ELException: /jsf/report/Edit.xhtml @39,99 listener="#{studentController.validate}": java.lang.NullPointerException
at com.sun.faces.facelets.el.TagMethodExpression.invoke(TagMethodExpression.java:111)
at com.sun.faces.facelets.tag.jsf.core.DeclarativeSystemEventListener.processEvent(EventHandler.java:131)
at javax.faces.component.UIComponent$ComponentSystemEventListenerAdapter.processEvent(UIComponent.java:2464)
at javax.faces.event.SystemEvent.processListener(SystemEvent.java:106)
at com.sun.faces.application.ApplicationImpl.processListeners(ApplicationImpl.java:2168)
at com.sun.faces.application.ApplicationImpl.invokeComponentListenersFor(ApplicationImpl.java:2116)
at com.sun.faces.application.ApplicationImpl.publishEvent(ApplicationImpl.java:288)
at com.sun.faces.application.ApplicationImpl.publishEvent(ApplicationImpl.java:246)
Caused by: java.lang.NullPointerException
at entities.StudentController.validate(StudentController.java:163)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
@ManagedBean()
@SessionScoped
public class StudentController implements Serializable {
public void validate(ComponentSystemEvent event) {
FacesContext fc = FacesContext.getCurrentInstance();
UIComponent components = event.getComponent();
UIInput uiName = (UIInput) components.findComponent("frmStudent:name");
UIInput uiClass = (UIInput) components.findComponent("frmStudent:myclass");
String name = uiName.getLocalValue().toString(); …
Run Code Online (Sandbox Code Playgroud) 我有一个用例,我需要计算两个字段的不同数量.
样品:
x = LOAD 'testdata' using PigStorage('^A') as (a,b,c,d);
y = GROUP x BY a;
z = FOREACH y {
**bc = DISTINCT x.b,x.c;**
dd = DISTINCT x.d;
GENERATE FLATTEN(group) as (a), COUNT(bc), COUNT(dd);
};
Run Code Online (Sandbox Code Playgroud) 这两个代码片段有什么区别?
小片1:
Object o = new Object();
int i = Objects.hashCode(o);
Run Code Online (Sandbox Code Playgroud)
摘录2:
Object o = new Object();
int i = o.hashCode();
Run Code Online (Sandbox Code Playgroud) 每当我们尝试serialize
一个Class的对象时,我们总是有一个唯一的值serialVersionId
作为私有final字段,它的意义是什么deserialization
,我们可以使用它来检查对象和值是否已经以适当的方式反序列化?
如果我运行以下代码行,我会收到DIVIDE BY ZERO
错误
1. System.out.println(5/0);
Run Code Online (Sandbox Code Playgroud)
这是预期的行为.
现在我运行下面的代码行
2. System.out.println(5/0F);
Run Code Online (Sandbox Code Playgroud)
这里没有DIVIDE BY ZERO
错误,而是显示INFINITY
在第一行中,我将两个整数和第二个两个实数分开.
为什么整数除以零会给出DIVIDE BY ZERO
错误,而在给出实数的情况下INFINITY
我确信它不是特定于任何编程语言.