这里有一些代码可以捕获Event Dispatch Thread上抛出的异常:
package com.ndh.swingjunk;
import java.awt.EventQueue;
import javax.swing.JFrame;
public class EntryPoint {
public static void main(String[] args) {
Thread.setDefaultUncaughtExceptionHandler(new MyExceptionHandler());
// System.setProperty("sun.awt.exception.handler", MyExceptionHandler.class.getName());
EventQueue.invokeLater(new Runnable()
{
public void run()
{
new SomeWindow("foo").setVisible(true);
}
});
}
}
class SomeWindow extends JFrame {
public SomeWindow(String title) {
this.setTitle(title);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
throw new RuntimeException("hello");
}
}
Run Code Online (Sandbox Code Playgroud)
我已经看到警告,事件调度线程抛出的异常不会被UncaughtExceptionHandler处理,但对我的例子来说似乎并非如此; 无论注册行是注释掉还是遗留下来,它的工作方式都是一样的.我的示例是以某种方式搞砸了,还是注册了sun.awt.exception.handler不再需要的异常处理程序?
我想让以下代码线程安全.实现它的最佳方法是什么?
private static final DateFormat DATE_FORMAT = DateFormat.getDateTimeInstance();
public static final String eventTypeToDateTimeString(long timestamp)
{
return DATE_FORMAT.format(new Date(timestamp));
}
Run Code Online (Sandbox Code Playgroud) 我在java.lang.Object中使用wait()的定时版本,并观察到它在两种不同的场景中的行为不同.
场景1:在Thread中使用run()的默认定义
public static void main (String[] args) throws InterruptedException {
Thread t = new Thread();
t.start();
System.out.print("X");
synchronized(t) { t.wait(10000);}
System.out.print("Y");
}
Run Code Online (Sandbox Code Playgroud)
关于scenario1的问题:我遇到了X和Y之间的延迟.这是因为我从main调用wait()(即使在t上)因此正在使用主线程的调用堆栈,而不是第二个线程?
场景2:动态 子类化线程以覆盖run()以打印内容.
public static void main (String[] args) throws InterruptedException {
Thread t = new Thread() {public void run()
{System.out.print("I am the second thread.");}};
t.start();
System.out.print("X");
synchronized(t) { t.wait(10000);}
System.out.print("Y");
}
Run Code Online (Sandbox Code Playgroud)
关于场景2的问题:我没有遇到任何延迟!是什么改变只是因为我已经覆盖了run()?现在,每次我运行该程序时,它立即打印出"XI am the second thread.Y",无论如何都没有任何延迟!wait()的效果在哪里消失了?
我有一个带有父类的已检查异常的方法,它可以抛出父类和子类的异常
public void method() throws ParentException {
if( false ) throw new ParentException();
else if( true ) throw new ChildException(); // this one is thrown
}
Run Code Online (Sandbox Code Playgroud)
我有一个级联catch块,它首先有子例外
try {
method();
} catch (ChildException e) {
// I get here?
} catch (ParentException e) {
// or here?
}
Run Code Online (Sandbox Code Playgroud)
哪个块会捕获抛出的异常?由于该方法仅显式声明了ParentException,因此ChildException是否会显示为ParentException的实例?
我正在学习java和一件我发现我不喜欢的东西,通常是我有这样的代码:
import java.util.*;
import java.io.*;
public class GraphProblem
{
public static void main(String[] args)
{
if (args.length < 2)
{
System.out.println("Error: Please specify a graph file!");
return;
}
FileReader in = new FileReader(args[1]);
Scanner input = new Scanner(in);
int size = input.nextInt();
WeightedGraph graph = new WeightedGraph(size);
for(int i = 0; i < size; i++)
{
graph.setLabel(i,Character.toString((char)('A' + i)));
}
for(int i = 0; i < size; i++)
{
for(int j = 0; j < size; j++)
{
graph.addEdge(i, j, …Run Code Online (Sandbox Code Playgroud) 我有一个方法需要一段时间才能完成.我希望我的方法在返回"最终"结果之前返回"初步"结果.
我想知道是否有可能这样的事情:
public Object myMethod () {
/*some computation here*/
return firstResult;
/*
very long computation here
*/
return finalResult;
}
Run Code Online (Sandbox Code Playgroud)
这甚至是可能的,还是可以建议某种解决方法?
请注意:我不是在征求您的意见.我问的是约定.
我只是想知道我是否应该兼得通过和未通过适当的方法名称,如测试,Should_Fail_When_UsageQuantityIsNegative(),Should_Fail_When_UsageQuantityMoreThan50(),Should_Pass_When_UsageQuantityIs50().
或者,我应该编码它们来传递并保持所有测试处于通过状态吗?
我有两个虚拟代码片段(让我们考虑它们是用Java或C#编写的,所有变量都是本地的):
代码1:
int a;
int b = 0;
for (int i = 1; i < 10 ; i++)
{
a = 10;
b += i;
// a lot of more code that doesn't involve assigning new values to "a"
}
Run Code Online (Sandbox Code Playgroud)
代码2:
int b = 0;
for (int i = 1; i < 10 ; i++)
{
int a = 10;
b += i;
// a lot of more code that doesn't involve assigning new values to "a"
}
Run Code Online (Sandbox Code Playgroud)
乍一看,我会说两个代码都消耗相同的内存量,但代码1的CPU效率更高,因为它只创建和分配变量a …
我目前正在使用Java的Observer/Observable Pattern,我想知道:为什么在当前的实现中需要setChanged()方法?我知道它就在这里,所以我们只需要在治疗结束时调用notifyObservers()一次.
这样,如果我们想要我们可以使用clearChanged()回滚任何更改.但是,我们仍然可以在我们自己的实现中进行所有检查,并且只在我们绝对想要更新Observers 时调用notifyObservers().
我可能错过了一些东西,但我不明白他们为什么不这样简化它.有任何想法吗?
关于使用 Spring JDBC,它工作得非常好,并且在使用批处理时比 JPA 有一些改进。
我很想了解当您已经拥有Spring Data JDBC 时为什么要使用 Spring Data JDBC。
我很想了解当您已经拥有Spring JDBC时为什么要使用R2DBC。
java ×8
awt ×1
c# ×1
concurrency ×1
exception ×1
r2dbc ×1
return ×1
spring ×1
spring-jdbc ×1
swing ×1
synchronized ×1
unit-testing ×1
wait ×1