Java线程疑问 [英] Java threads doubt

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

问题描述

我早些时候发布了一个关于 Java 线程的查询.( 链接文本)

I had earlier posted a query on Java threads. ( link text)

根据我收到的答案,我决定实施它们.所以我在一台有 2 个 cpu 内核的机器上完成了这一点编码.代码如下

And based on the answers i received, i decided to implement them. So ive done this bit of coding on a machine with 2 cpu cores. The code is as follows

import java.net.*;
import java.io.*;


public class thready implements Runnable{
private Socket num;

public thready(Socket a) {
    this.num=a;
}
public void run() {
    try {
        BufferedInputStream is = new BufferedInputStream(num.getInputStream());
        System.out.println("Connected to port"+num);
    } catch (IOException ex) {
        //Logger.getLogger(thready.class.getName()).log(Level.SEVERE, null, ex);
    }


}
public static void main(String [] args)
{
    int port = 80;
    int port1= 81;
    //int count = 0;
     try{

    ServerSocket socket1 = new ServerSocket(port);
    ServerSocket socket2 = new ServerSocket(port1);
    while (true) {
    Socket connection = socket1.accept();
    Socket connection1 = socket2.accept();

    Runnable runnable =new thready(connection);
    Runnable run= new thready(connection1);
    Thread t1=new Thread(runnable);
    Thread t2=new Thread(run);
    t1.start();
    t2.start();
    }
     }
     catch(Exception e)
     {

     }   }}

现在我使用超级终端测试这段代码并连接到端口 890 和端口 81(我使用超级终端的 2 个实例),据我了解,预期行为应该是连接到端口‘端口号’" 应在连接到任何端口(80 或 81)后立即打印.但是我从这段代码中得到的输出是,如果我只连接到 1 个端口,则不会打印所需的输出,如果我一个接一个地连接到两个端口,则输出仅在两个端口后打印连接.所以这再次让我产生了最初的困惑,即这两个线程是同时执行还是在这两个线程之间交替执行.

Now Im testing this piece of code using Hyperterminal and am connecting to both port 890 and port 81(am using 2 instances of the hyperterminal) and as i understand it the expected behavior should be that "Connected to port 'port number'" should be printed as soon as a connection to any port( 80 or 81) is made. But the output that im getting here from this piece of code is that if i connect to only 1 port then the required output is not getting printed and if i connect to both ports, one after the other, the output is printed only after both ports are connected. So this again leads me to the initial confusion as to whether both these threads are executing concurrently or the execution is alternating between these 2 threads.

任何建议都会有很大帮助.

Any suggestions would be of great help.

干杯

推荐答案

您在启动线程之前调用了 accept.accept 将阻塞,直到建立连接,这就是为什么你会看到你所做的行为.如果要侦听多个端口,则需要[1] 为每个 ServerSocket 创建一个线程,然后在 accept 返回时启动通信线程或处理线程中的一个一个连接进行监听.

You're calling accept before starting the threads. accept will block until a connection is made, so that's why you're seeing the behavior you do. If you want to listen on multiple ports, you will need to[1] create a thread for each ServerSocket and then either start a communication thread when the accept returns or process the connections one by one in the thread doing the listening.

[1] 这仅适用于您直接使用 ServerSocket 的情况,您可能应该在学习时使用它.java.nio 包及其子包包含用于多路复用非阻塞 I/O 的类,可用于例如侦听同一线程中的多个套接字.

[1] This applies only if you are using ServerSocket directly, which you probably should be using while learning. The java.nio package and its subpackages contain classes for use with multiplexing non-blocking I/O that can be used to, e.g., listen on multiple sockets in the same thread.

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

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