When a thread is running, how to notify the main thread?

I use FTP raw commands to upload a file to an FTP server, I start a new stream to send a file through a socket to my code. when a recently started thread has finished sending the file, I want to display some message on the console, how can I make sure that the thread has finished working? 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();
+5
source share
2 answers

How can I make sure the thread has completed work?

You call Thread.join()as follows:

...
Thread t = new Thread(new FTPFileSender(pasvHost, pasvPort, fileToSend));
t.start();

// wait for t to finish
t.join();

Please note that it Thread.joinwill be blocked until the completion of another thread.

, , upload UploadThread, , . , , addUploadListener , . - :

UploadThread ut = new UploadThread(...);
ut.addUploadListener(new UploadListener() {
    public void uploadComplete() {
        System.out.println("Upload completed.");
    }
});

ut.start();
+9

, , :

  • - join . , - join, .
  • - , Thread . , .
+2

All Articles