在Java中停止线程? [英] Stopping a Thread in Java?

查看:87
本文介绍了在Java中停止线程?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一段连接到服务器的代码,使用该连接生成一堆线程并执行一堆东西。

I'm in the process of writing a piece of code that connects to a server spawns a bunch of threads using that connection and does a bunch of "stuff".

在某些情况下,连接失败,我需要停止所有操作并从头开始使用新对象。

There are certain instances where the connection fails and I need to stop everything and start from scratch with a new object.

我想在对象之后清理但是调用线程上的thread.stop,但这个方法似乎已被弃用。

I wanted to clean up after the object but calling thread.stop on the threads, but this method is seemingly deprecated.

建议的替代方法是什么?我应该为每个线程编写自己的清理和退出方法吗?将线程设置为null?或者是其他东西?

What is the recommended alternative to doing this? Should I write my own cleanup and exit method for each of the threads? Set the thread to null? or something else?

推荐答案

请看这里:

停止线程的建议方法 HowToStopAThread

这篇关于在Java中停止线程?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆