求与佛有染书包网百度网盘txt

当前位置: →
→ &Java SE& 多线程 run方法中异常处理-
&Java SE& 多线程 run方法中异常处理-
& 作者及来源: 阿童沐 - 博客园 &
&收藏到→_→:
摘要: &Java SE& 多线程 run()方法中异常处理-
"&Java SE& 多线程 run方法中异常处理-"::
thread的run 是不抛出任何检查型异常(checked exception)的,但是它自身却可能因为一个异常而被终止,导致这个线程的终结。最麻烦的是,在线程中抛出的异常即使使用try...catch也无法截获,因此可能导致一些问题出现,比如异常的时候无法回收一些资源,或者没有关闭当前的连接等等。&
jdk5.0之前,不能为单独的thread设置uncaughtexceptionhandler,也不能指定一个默认的uncaughtexceptionhandler。为了可以设置一个uncaughtexceptionhandler,需要去继承threadgroup并覆写uncaughtexception 。
在jdk5.0中,我们通过thread的实例 setuncaughtexceptionhandler,可以为任何一个thread设置一个uncaughtexceptionhandler。当然你也可以为所有thread设置一个默认的uncaughtexceptionhandler,通过调用thread.setdefaultuncaughtexceptionhandler(thread.uncaughtexceptionhandler eh) ,这是thread的一个static 。&
定义一个handler类必须实现thread.uncaughtexceptionhandler接口的void uncaughtexception(thread t, throwable e) 。如果不设置一个handler,那么单个thread的handler是null。但是,如果这个单个线程是threadgroup中的一个thread,那么这个线程将使用threadgroup的uncaughtexceptionhandler。threadgroup自身已经实现了thread.uncaughtexceptionhandler接口。
package com.
import java.lang.thread.unca
public class threadtest {
public static void main(string[] args) {
errhandler handle = null;
threada a = null;
a = new threada();
handle = new errhandler();
a.setuncaughtexceptionhandler(handle);//加入定义的errhandler
a.start();
} catch (exception e) {
handle.uncaughtexception(a, e);//捕获到了!!!
//普通线程即使使用try...catch也无法捕获到抛出的此文来自: 马开东博客
转载请注明出处 网址:
异常,因为异常是发生在新的线程中
threadb b = new threadb();
b.start();
} catch (exception e) {
e.getmessage();//不起作用
* 自定义的一个uncaughtexceptionhandler
class errhandler implements uncaughtexceptionhandler {
这里可以做任何针对异常的处理,比如记录日志等等
public void uncaughtexception(thread a, throwable e) {
system.out.println("this is:" + a.getname() + ",message:" + e.getmessage());
e.printstacktrace();
* 拥有uncaughtexceptionhandler的线程
class threada extends thread {
private thread.uncaughtexce
public threada() {
public void run() {
double i = 12 / 0;//抛出异常的地方
* 普通线程
class threadb extends thread {
public threadb() {
public void run() {
double i = 12 / 0;//抛出异常的地方
简洁的代码:
package cn.edu.bupt.
public class threadtest
public static void main(string[] args)
errhandler handler = new errhandler();
thread th = new thread(new threaddividebyzero());
th.setuncaughtexceptionhandler(handler);
th.start();
* 线程的run() 中发生异常的时候,将会转到该类中的uncaughtexception 中执行一些善后操作,流入释放资源
* @author medow
class errhandler implements thread.uncaughtexceptionhandler
* 这里可以防止一些当线程发生异常的时候,对资源进行释放的代码,
public void uncaughtexception(thread t, throwable e)
system.out.println("errhandler here, u can put some code here which is used to release related resource");
e.printstacktrace();
* @author medow
class threaddividebyzero implements runnable
public void run()
system.out.println(5 / 0);
// 将会抛出异常的地方
参考链接:http://blog.马开东/realasker/article/details/2132184
     &http://www./yxsz/jjglxy/book/java_api/java/lang/thread.uncaughtexceptionhandler.html
&搜索此文相关文章:
SE& 多线程 run 中异常处理-此文来自: 马开东博客
网址: 站长QQ
&Java SE& 多线程 run方法中异常处理-_博客园相关文章
博客园_总排行榜
博客园_最新
博客园_月排行榜
博客园_周排行榜
博客园_日排行榜Java中线程的start方法和run方法的区别 - 推酷
Java中线程的start方法和run方法的区别
有时间真的要好好的看一下Java的多线程了,太重要了,都怪我学的时候没好好学.
有一个需求就是别人调用我写的一个接口,然后我去执行一系列的处理,无奈要处理的数据非常多,而且执行会非常慢.别人是使用httpclient调用的,如果长时间我不返回,就会造成超时.所以我这边需要做的就是,再启一个线程执行那个非常耗时的方法,先返回数据给对方.想起了使用多线程去做.
也按照自己之前对多线程的理解去做了,但是发现还是没实现多想要的效果.代码如下
package org.linuxsogood.
public class TestMutiThread {
public static void main(String[] args) {
new Thread(new Runnable() {
public void run() {
for (int i = 0; i & 20; i++) {
System.out.println(i+&hello.&);
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
new Thread(new Runnable() {
public void run() {
for (int i = 0; i & 20; i++) {
System.out.println(i+&test.&);
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
System.out.println(&test end&);
搞了半天也没搞明白哪里有问题,哎,从头再学一遍吧,又找了一些基础的例子看了一下.发现是正确的,但是调用的方法是start而不是run方法. 于是把我的代码里的run改成start,发现真的好了.
那到底run和start方法到底有什么区别呢?
来查看一下源码
* Causes this threa the Java Virtual Machine
* calls the &code&run&/code& method of this thread.
* The result is that two threads are running concurrently: the
* current thread (which returns from the call to the
* &code&start&/code& method) and the other thread (which executes its
* &code&run&/code& method).
* It is never legal to start a thread more than once.
* In particular, a thread may not be restarted once it has completed
* execution.
* @exception
IllegalThreadStateException
if the thread was already
public synchronized void start() {
* This method is not invoked for the main method thread or &system&
* group threads created/set up by the VM. Any new functionality added
* to this method in the future may have to also be added to the VM.
* A zero status value corresponds to state &NEW&.
if (threadStatus != 0)
throw new IllegalThreadStateException();
/* Notify the group that this thread is about to be started
* so that it can be added to the group's list of threads
* and the group's unstarted count can be decremented. */
group.add(this);
boolean started =
} finally {
if (!started) {
group.threadStartFailed(this);
} catch (Throwable ignore) {
/* do nothing. If start0 threw a Throwable then
it will be passed up the call stack */
private native void start0();
当一个线程启动的时候,它的状态被调为了0,如果不是0的话会抛异常出来.然后把这个线程加入到了线程组.最后尝试调用start0方法,而start0方法是私有的,私有的一般是使用C语言写的.看来要调用系统底层,还是要通过C.
个人猜测:Java中的多线程,其实底层是使用C代码来实现的.这也就是为什么调用线程中的start方法可以实现多线程的效果.而如果调用run方法的话,其实调用的就是自己写的那个runnable里,自己实现的那个run方法.
我们看下Thread里run的源码
public void run() {
if (target != null) {
target.run();
如果target不为空的话,调用target的run方法.再看一下target是什么
/* What will be run. */
那,其实就是一个Runnable.到此证实了我的猜测
已发表评论数()
请填写推刊名
描述不能大于100个字符!
权限设置: 公开
仅自己可见
正文不准确
标题不准确
排版有问题
主题不准确
没有分页内容
图片无法显示
视频无法显示
与原文不一致(IT行业交流)
(IT行业交流)
(IT行业交流)
(IT行业交流)
(IT行业交流)
第三方登录:在java线程的run方法里在new多个线程可以吗? - ITeye问答
在java线程的run方法里在new多个线程可以吗,我要写个定时器定时执行一个线程,可以不可以在这个线程的run方法里在new多个线程执行任务,执行的结果在run方法里汇总然后返回问题补充:to 和你在一起:我用了TimerTask,然后用join等每个线程执行完后返回结果好像可以吗
采纳的答案
1.可以new多个
2.run方法不能带返回值
3.可以用集合类保存你生成的线程
你应该用ExecutorService来管理多个任务
当然可以在这个线程的run方法里在new多个线程执行任务,
但是这样new出来的线程不受这个线程的控制啊,
已解决问题
未解决问题}

我要回帖

更多关于 与佛有染 的文章

更多推荐

版权声明:文章内容来源于网络,版权归原作者所有,如有侵权请点击这里与我们联系,我们将及时删除。

点击添加站长微信