here's a little code I wrote. This next class waits for a connection and creates a new thread upon receiving one:
ServerSocket serverSocket = null;
ExecutorService serv = Executors.newCachedThreadPool();
serv.execute(new UserThread());
try {
serverSocket = new ServerSocket(FMDataManager.getPort());
serverSocket.setSoTimeout(0);
while (_listening){
System.out.println("Listening on port "+FMDataManager.getPort());
System.out.println("Waiting for connections.");
serv.execute(new UploadThread(serverSocket.accept()));
}
} catch (IOException e) {
System.err.println("Could not listen on port: "+FMDataManager.getPort()+".");
System.exit(-1);
}
as you can see i am using ServerSocket.accept() method to wait for a connection. The thread is indeed created, but it won't run. I put a little "thread created" in its constructor and another message "starting thread" in run(), but I only got the first message. After that it didn't do anything, I didn't even get "thread created". Any ideas please?
I've add the implementation of the UploadThread I am trying to start, maybe It'll help
public class UploadThread extends Thread{
Socket _socket;
public UploadThread(Socket socket) {
super("UserThread");
_socket = socket;
}
public void run(Socket socket) {
System.out.println("entred upload thread");
DataOutputStream out = null;
DataInputStream in = null;
try {
out = new DataOutputStream(_socket.getOutputStream());
in = new DataInputStream(_socket.getInputStream());
FileMessage inputMessage;
SendFile outputMessage;
inputMessage = (FileMessage) CommandEnum.readMessage(in);
System.out.println("F: "+inputMessage.getCaption());
File file = null;
Iterator<File> itr = FMDataManager.getFiles().iterator();
while (itr.hasNext()){
File temp = itr.next();
if (temp.getName().equals(inputMessage.getFile()))
file = temp;
}
outputMessage = new SendFile(file);
outputMessage.send(out);
} catch (IOException e) {
e.printStackTrace();
} finally{
try {
_socket.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
Try
serv.submit
instead of
serv.execute
.
EDIT
Looks like UploadThread
isn't overriding run()
correctly. That being said, your run method declaration should look like this:
@Override
public void run(){
//do stuff
}
There's no need to pass socket
as an argument.
As indicated by sthupahsmaht's comment your UploadThread
implementation is wrong. The signature for the run()
method is public void run()
. Currently you are creating a new method with the signature public void run(Socket)
. As run()
doesn't take any arguments you have to pass all parameters via the constructor or setters.
There are two best practices that can help you avoid such mistakes in the future:
@Override
. If you create a new method with @Override
, the compiler signals an error. Thread
but implement Runnable
. Thread
has a default implementation for run()
which does nothing. This is what happens with your code currently. Without seeing the part of the code that actually constructs the thread, I'll guess you're NOT calling the 'start()' method on the thread object.
Needs to be something like this:
Thread thr = new Thread(new Runnable() { void run() { /* do stuff */ }) ;
thr.start() // GO!
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.