我有Java主类,在类中,我启动一个新线程,在主,它等待直到线程死亡.在某些时刻,我从线程中抛出一个运行时异常,但是我无法捕获从主类中的线程抛出的异常.
这是代码:
public class Test extends Thread
{
public static void main(String[] args) throws InterruptedException
{
Test t = new Test();
try
{
t.start();
t.join();
}
catch(RuntimeException e)
{
System.out.println("** RuntimeException from main");
}
System.out.println("Main stoped");
}
@Override
public void run()
{
try
{
while(true)
{
System.out.println("** Started");
sleep(2000);
throw new RuntimeException("exception from thread");
}
}
catch (RuntimeException e)
{
System.out.println("** RuntimeException from thread");
throw e;
}
catch (InterruptedException e)
{
}
}
}
Run Code Online (Sandbox Code Playgroud)
谁知道为什么?
我有一个名为的方法的对象StartDownload(),它启动三个线程.
如何在每个线程执行完毕后收到通知?
有没有办法知道一个(或全部)线程是完成还是仍在执行?
嗨,我正在尝试在我的应用程序中实现java日志记录.我想使用两个处理程序.文件处理程序和我自己的控制台处理程序 我的两个处理程序都运行良好.我的日志记录将发送到文件和控制台.我的日志记录也被发送到我不想要的默认控制台处理程序.如果您运行我的代码,您将看到发送到控制台的额外两行.我不想使用默认的控制台处理程序.有谁知道如何禁用默认控制台处理程序.我只想使用我创建的两个处理程序.
Handler fh = new FileHandler("test.txt");
fh.setFormatter(formatter);
logger.addHandler(fh);
Run Code Online (Sandbox Code Playgroud)
Handler ch = new ConsoleHandler();
ch.setFormatter(formatter);
logger.addHandler(ch);
Run Code Online (Sandbox Code Playgroud)
import java.util.Date;
import java.util.logging.ConsoleHandler;
import java.util.logging.FileHandler;
import java.util.logging.Formatter;
import java.util.logging.Handler;
import java.util.logging.Level;
import java.util.logging.LogManager;
import java.util.logging.LogRecord;
import java.util.logging.Logger;
public class LoggingExample {
private static Logger logger = Logger.getLogger("test");
static {
try {
logger.setLevel(Level.INFO);
Formatter formatter = new Formatter() {
@Override
public String format(LogRecord arg0) {
StringBuilder b = new StringBuilder();
b.append(new Date());
b.append(" ");
b.append(arg0.getSourceClassName());
b.append(" ");
b.append(arg0.getSourceMethodName());
b.append(" ");
b.append(arg0.getLevel());
b.append(" ");
b.append(arg0.getMessage()); …Run Code Online (Sandbox Code Playgroud) 我使用Jersey和Jackson在Glassfish 3.1.2下运行RESTful Web服务:
@Stateless
@LocalBean
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
@Path("users")
public class UserRestService {
private static final Logger log = ...;
@GET
@Path("{userId:[0-9]+}")
public User getUser(@PathParam("userId") Long userId) {
User user;
user = loadUserByIdAndThrowApplicableWebApplicationExceptionIfNotFound(userId);
return user;
}
}
Run Code Online (Sandbox Code Playgroud)
对于预期的异常,我抛出适当的WebApplicationException,我对发生意外异常时返回的HTTP 500状态感到满意.
我现在想为这些意外的异常添加日志记录,但是尽管搜索,但是无法找到我应该如何处理这个问题.
我已经尝试使用a Thread.UncaughtExceptionHandler并且可以确认它是在方法体内应用的,但是它的uncaughtException方法永远不会被调用,因为其他东西在它们到达我的处理程序之前处理未捕获的异常.
我见过一些人使用的另一个选项是ExceptionMapper,它捕获所有异常,然后过滤掉WebApplicationExceptions:
@Provider
public class ExampleExceptionMapper implements ExceptionMapper<Throwable> {
private static final Logger log = ...;
public Response toResponse(Throwable t) {
if (t instanceof WebApplicationException) {
return ((WebApplicationException)t).getResponse();
} …Run Code Online (Sandbox Code Playgroud)