I use FTP raw commands to upload file to a FTP server, I start a new thread to send file via socket in my code. when the newly started thread finished sending file I want to output some message to console, how can I make sure the thread have finished it’s work ?
here is my code:
TinyFTPClient ftp = new TinyFTPClient(host, port, user, pswd);
ftp.execute("TYPE A");
String pasvReturn = ftp.execute("PASV");
String pasvHost = TinyFTPClient.parseAddress(pasvReturn);
int pasvPort = TinyFTPClient.parsePort(pasvReturn);
new Thread(new FTPFileSender(pasvHost, pasvPort, fileToSend)).start();
You do call
Thread.join()like this:Note however that
Thread.joinwill block until the other thread has finished.A better idea is perhaps to encapsulate the upload-thread in a
UploadThreadclass which performs some callback when it’s done. It could for instance implement anaddUploadListenerand notify all such listeners when the upload is complete. The main thread would then do something like this: