ServerWorker 类型的 start() 方法未定义...... (java Runnable)

发布于 2024-10-11 21:42:21 字数 1327 浏览 8 评论 0原文

好吧,我之前用 Java 做过线程,但我认为应该正确的事情给了我这个错误


package com.mdog.tcpserver;
import java.net.*;
import java.io.*;

public class ServerDriver {


     public static void main(String[] args) throws IOException {

         ServerSocket serverSocket = null;
         boolean listening = true;

         /* bind welcome socket to port */
         try {
             serverSocket = new ServerSocket(1313);
         } catch (IOException e) {
             System.err.println("Could not listen on port: 1313.");
             System.exit(1);
         }


         /* assign thread to each client */
         while(listening){
         new ServerWorker(serverSocket.accept()).start();
         }

         /* close the socket and quit */
         serverSocket.close();
         return;
 }
}





---------


package com.mdog.tcpserver;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;

public class ServerWorker implements Runnable {

    private Socket socket = null;

 public ServerWorker(Socket s){
  super();
  this.socket = s;
 }



 public void run() {

         (stuff happens)

    } 
}



错误是在这一行“new ServerWorker(serverSocket.accept()).start();”它说...“方法 start() 对于类型 ServerWorker 来说是未定义的”

Ok, I've done threading with Java before but something that I think should be correct is giving me this error


package com.mdog.tcpserver;
import java.net.*;
import java.io.*;

public class ServerDriver {


     public static void main(String[] args) throws IOException {

         ServerSocket serverSocket = null;
         boolean listening = true;

         /* bind welcome socket to port */
         try {
             serverSocket = new ServerSocket(1313);
         } catch (IOException e) {
             System.err.println("Could not listen on port: 1313.");
             System.exit(1);
         }


         /* assign thread to each client */
         while(listening){
         new ServerWorker(serverSocket.accept()).start();
         }

         /* close the socket and quit */
         serverSocket.close();
         return;
 }
}





---------


package com.mdog.tcpserver;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;

public class ServerWorker implements Runnable {

    private Socket socket = null;

 public ServerWorker(Socket s){
  super();
  this.socket = s;
 }



 public void run() {

         (stuff happens)

    } 
}



The error is on this line "new ServerWorker(serverSocket.accept()).start();" and it says... "The method start() is undefined for the type ServerWorker"

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

◇流星雨 2024-10-18 21:42:22

新线程(new ServerWorker(serverSocket.accept())).start()

http://download.oracle.com/javase/1.4.2/docs/api/java/lang/Thread.html#start%28 %29

new Thread(new ServerWorker(serverSocket.accept())).start()

http://download.oracle.com/javase/1.4.2/docs/api/java/lang/Thread.html#start%28%29

我做我的改变 2024-10-18 21:42:22

抱歉我不知道如何在评论中添加评论。但

在 while 循环中启动新线程???我想这段代码会在一秒钟内崩溃

是错误的。 Accept() 块。为每个新套接字(也称为连接客户端)创建新线程(或者正如已经给出的正确答案指出的那样)。与任何其他循环相比,在 while 循环中启动新线程没有任何问题,只要在本例中是适当的即可。

Sorry I don't know how to add comments to comments. But

Starting new thread in a while cycle??? This code will crash in a second, I suppose

is wrong. accept() blocks. new threads are created (or would be as the correct answer already given points out) for each new socket (aka connecting client). There's nothing wrong with starting new threads in a while loops vs any other loop as long as it's appropriate which in this case it is.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文