ServerWorker 类型的 start() 方法未定义...... (java Runnable)
好吧,我之前用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
新线程(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
抱歉我不知道如何在评论中添加评论。但
是错误的。 Accept() 块。为每个新套接字(也称为连接客户端)创建新线程(或者正如已经给出的正确答案指出的那样)。与任何其他循环相比,在 while 循环中启动新线程没有任何问题,只要在本例中是适当的即可。
Sorry I don't know how to add comments to comments. But
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.