求解,关于Thread的join问题
public class DownLoadFrame extends Thread{
Socket socket4 = null;
int port4 = 21000;
String serverIP4;
FileOutputStream fos;
DataInputStream dis;
OutputStream os4;
InputStream is4;
private JFrame downloadframe = new JFrame();
private JList<String> downloadlist = new JList<String>();
private JScrollPane downloadpane = new JScrollPane(downloadlist);
String[] filelist;
String selectedfile;
public void run() {
try {
filelist=null;
selectedfile=null;
InetAddress inet = InetAddress.getLocalHost();
serverIP4=inet.getHostAddress();
socket4 = new Socket(serverIP4,port4);
System.out.println("文件下载服务连接成功!");
os4=socket4.getOutputStream();
is4=socket4.getInputStream();
int filelistlength = is4.read();
filelist = new String[filelistlength];
for(int i=0;i<filelistlength;i++) {
byte[] bt = new byte[1024];
int n = is4.read(bt);
filelist[i]=new String(bt,0,n);
}
Runnable downloader = new Runnable() {
public void run() {
downloadframe.setSize(300,200);
downloadframe.add(downloadpane);
downloadframe.setVisible(true);
downloadframe.setTitle("双击选择下载文件");
downloadlist.setListData(filelist);
MouseListener mouseListener = new MouseAdapter() {
public void mouseClicked(MouseEvent e) {
if (e.getClickCount() == 2) {
selectedfile = downloadlist.getSelectedValue();
downloadframe.dispose();
}
}
};
downloadlist.addMouseListener(mouseListener);
}
} ;
Thread download = new Thread(downloader);
download.start();
download.join();//等待子线程结束
System.out.println(selectedfile);
求解,按道理来说调用了join()方法之后要等到这个子线程结束了之后才会继续主线程,我的设想是在窗口程序内选择了一个值,把这个值输入给变量,然后关闭窗口,再把这个值打印出来,可是这里程序运行时join像是根本没有起作用一样,直接输出了一个空字符null,这是为什么呢?