gpt4 book ai didi

java - 在线程上调用方法 - 它可以工作,但为什么呢?

转载 作者:行者123 更新时间:2023-12-01 10:08:20 25 4
gpt4 key购买 nike

我有以下 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) 当您调用您的

send(Object data)

你不是从你的线程中执行此操作,因此请记住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 :: ⇠ run [10003ms]

关于java - 在线程上调用方法 - 它可以工作,但为什么呢?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36312717/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com