简体   繁体   English

在catch中开始一个新线程

[英]start a new thread in catch

I have a class ( class A for example) implements Runnable . 我有一个类(例如class A )实现了Runnable In run method I have a try catch .I want to start a new thread in catch like this 在run方法中我有一个try catch 。我想在这样的catch中启动一个新thread

new Thread(new A()).start();

Is this a true manner to handle exceptions? 这是处理异常的真实方式吗?

I mean maybe its a dangerous way because the heap will get full very soon; 我的意思是也许这是一种危险的方式,因为堆很快就会满了; in other words garbage collector will not garbage this object because another object has been just created in it. 换句话说, garbage collector不会垃圾这个object因为另一个对象刚刚在其中创建。

I mean maybe its a dangerous way because the heap will get full very soon; 我的意思是也许这是一种危险的方式,因为堆很快就会满了; in other words garbage collector will not garbage this object because another object has been just created in it. 换句话说,垃圾收集器不会垃圾这个对象,因为另一个对象刚刚在其中创建。

It is not dangerous for that reason. 这个原因并不危险。 If we assume that new Thread(new A()).start(); 如果我们假设new Thread(new A()).start(); is the last thing that the original thread does before it exits, then by the time we need to GC the original thread will have exited, and hence its stack contents won't be reachable. 是原始线程在退出之前做的最后一件事,然后当我们需要GC时,原始线程将退出,因此它的堆栈内容将无法访问。 The only thread that will still be reachable will be the one that it still alive. 唯一仍然可以访问的线程将是它仍然存在的线程。

However, it is dangerous if the new thread is liable to repeat the computation and then throw the same exception again, and again, and again ... So if you do write code like this, it is a good idea for the application to keep a track of how often the thread is being re-launched, and pull the plug if it happens too often. 但是,如果新线程可能会重复计算然后再次抛出相同的异常,那么这将是危险的,所以如果你确实编写了这样的代码,那么应用程序保持这样做是个好主意。跟踪线程重新启动的频率,如果频繁发生则拔掉插头。

The other problem with the code as written is that the code that launched the original thread sees it die, but doesn't hear about the new thread. 编写代码的另一个问题是启动原始线程的代码看到它死了,但没有听到新线程。 That is problematic if your want to initiate shutdown by interrupting the worker threads. 如果您想通过中断工作线程来启动关闭,那么这是有问题的。

If you put those two problems (and others) together, it is better for the code that launched the original thread to be responsible for relaunching. 如果将这两个问题(和其他问题)放在一起,那么启动原始线程的代码最好负责重新启动。

Thread is new parallel light weight process. 螺纹是新的平行轻量化工艺。 As soon as its run method completed it will be eligible for GC. 一旦其运行方法完成,它将有资格用于GC。 I don't think it effects GC life cycle of the object from where it started. 我认为它不会影响对象的GC生命周期。

Only one new thing in your case is, handling exceptions with thread. 在您的情况下,只有一个新的事情是,使用线程处理异常。 Without knowing more details about why you want this, its hard to tell is it safe/good practice. 如果不了解您想要的更多细节,很难说它是安全/良好的做法。

This is not a very good way of handling exceptions within a thread. 这不是处理线程内异常的好方法。 Why would the newly created thread of the same type not have the same exception? 为什么新创建的相同类型的线程没有相同的异常?

What you should do is have some form of thread manager up a level from the thread that will monitor for, handle, and if necessary recreate new threads when old ones fail. 你应该做的是从线程中获得某种形式的线程管理器,该线程将监视,处理,并在必要时在旧线程失败时重新创建新线程。

This will allow you to add more ways to handle the error, and will look a lot neater if you try and debug the threads. 这将允许您添加更多方法来处理错误,如果您尝试调试线程,它将看起来更整洁。 instead of having all these hanging threads (cause the parent was cleaned by GC) you'll know all threads have spawned from the same location. 而不是拥有所有这些挂起的线程(因为父进程被GC清理),你将知道所有线程都是从同一位置生成的。

What you are proposing will not clutter the heap because threads will be GC'd when they have finished running. 您提出的建议不会使堆混乱,因为线程在运行完毕后将进行GC。

If you didn't store any references to the thread that you'd created - it will be cleaned by GC when terminated. 如果您没有存储对您创建的线程的任何引用 - 终止时将由GC清除它。 In your case I think it's pretty safe to start a new thread inside run() method. 在你的情况下,我认为在run()方法中启动一个新线程是相当安全的。

Just be sure you are not creating inner classes or storing this thread instance - it can cause memory leak, of course. 确保你没有创建内部类或存储这个线程实例 - 当然它可能导致内存泄漏。

Good luck 祝好运

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM