我有以下Thread子类(为便于阅读而略作简化):
public class ConnectionHandlerThread extends Thread {
private Socket socket;
private volatile boolean disconnected = false;
private ObjectOutputStream out = null;
private ObjectInputStream in = null;
public ConnectionHandlerThread(Socket socket){
this.socket = socket;
}
public void run(){
disconnected = false;
try {
out = new ObjectOutputStream(socket.getOutputStream());
in = new ObjectInputStream(socket.getInputStream());
while(!disconnected){
try{
Object data = in.readObject();
}
catch(ClassNotFoundException e){
// Handle exception
}
catch(IOException e){
// Handle exception
}
}
}
catch (IOException e) {
// Handle exception
}
}
public void send(Object data){
try{
out.writeObject( data );
}
catch(IOException e){
// Handle exception
}
}
}
当我连接到服务器时,该线程的实例由我的客户端(使用Swing GUI)创建。我感到奇怪的是,我可以从主GUI调用方法
send(Object data)
,并且该方法有效。为什么无限的while循环和/或对in.readObject()
的调用不能阻止我这样做?我的意思是线程不应该一直忙于做其他事情吗?这是做事的好方法吗?如果没有,为什么不呢?编辑
为了弄清是什么使我感到困惑:如果在主线程中,它将在该
in.readObject()
上忙于读取某些内容,然后它将在循环的下一次迭代中再次开始侦听。当然,我知道可以从另一个线程调用send()
。但是,我的主意是-“谁”实际上在执行send()
?我的线程正在执行此操作,还是调用线程正在执行此操作?如果是前者,那么如何才能同时在while循环中等待输入并执行send()
方法呢?我很难全神贯注于此... 最佳答案
有两件事:
1)无限循环不会使cpu只忙于自身。当它可用时,它只是在不断保持忙碌,但是其他线程也使用它。
2)当您致电
发送(对象数据)
您不要从线程中执行此操作,因此请记住1)调用它没有什么奇怪的
例:
码:
public class Main {
public static void main(String[] args) {
InfiniteThread t = new InfiniteThread();
t.start();
for(int i=0;i<10;i++) {
t.fromOut(i);
}
}
@DebugLog
public static class InfiniteThread extends Thread {
public void run() {
for(int i=0;i<10;i++) {
fromIn();
}
}
private void fromIn() {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
public void fromOut(Object data){
try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
输出:
InfiniteThread ::⇢()[Thread:“ main”] InfiniteThread ::⇠
[0ms] InfiniteThread ::⇢run()[Thread:“ Thread-0”]
InfiniteThread ::⇢fromIn()[Thread:“ Thread-0”] InfiniteThread ::⇢
fromOut(data = 0)[Thread:“ main”] InfiniteThread ::⇠fromOut [500ms]
InfiniteThread ::⇢fromOut(data = 1)[Thread:“ main”] InfiniteThread ::
⇠fromIn [1000ms] InfiniteThread ::⇢fromIn()[Thread:“ Thread-0”]
InfiniteThread ::⇠fromOut [500ms] InfiniteThread ::⇢
fromOut(data = 2)[Thread:“ main”] InfiniteThread ::⇠fromOut [500ms]
InfiniteThread ::⇢fromOut(data = 3)[Thread:“ main”] InfiniteThread ::
⇠fromIn [1000ms] InfiniteThread ::⇢fromIn()[Thread:“ Thread-0”]
InfiniteThread ::⇠fromOut [500ms] InfiniteThread ::⇢
fromOut(data = 4)[Thread:“ main”] InfiniteThread ::⇠fromOut [500ms]
InfiniteThread ::⇢fromOut(data = 5)[Thread:“ main”] InfiniteThread ::
⇠fromIn [1000ms] InfiniteThread ::⇢fromIn()[Thread:“ Thread-0”]
InfiniteThread ::⇠fromOut [500ms] InfiniteThread ::⇢
fromOut(data = 6)[Thread:“ main”] InfiniteThread ::⇠fromOut [500ms]
InfiniteThread ::⇢fromOut(data = 7)[Thread:“ main”] InfiniteThread ::
⇠fromIn [1000ms] InfiniteThread ::⇢fromIn()[Thread:“ Thread-0”]
InfiniteThread ::⇠fromOut [500ms] InfiniteThread ::⇢
fromOut(data = 8)[Thread:“ main”] InfiniteThread ::⇠fromOut [500ms]
InfiniteThread ::⇢fromOut(data = 9)[Thread:“ main”] InfiniteThread ::
⇠fromIn [1000ms] InfiniteThread ::⇢fromIn()[Thread:“ Thread-0”]
InfiniteThread ::⇠fromOut [500ms] InfiniteThread ::⇠fromIn
[1000ms] InfiniteThread ::⇢fromIn()[Thread:“ Thread-0”]
InfiniteThread ::⇠fromIn [1000ms] InfiniteThread ::⇠fromIn()
[Thread:“ Thread-0”] InfiniteThread ::⇠fromIn [1000ms] InfiniteThread
::⇢fromIn()[Thread:“ Thread-0”] InfiniteThread ::⇠fromIn [1000ms]
InfiniteThread ::⇢fromIn()[Thread:“ Thread-0”] InfiniteThread ::⇠
fromIn [1000ms] InfiniteThread ::运行[10003ms]