求解,关于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,这是为什么呢?
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,这是为什么呢?
...全文
相关推荐
c++11中关于std::thread的join的详解
主要介绍了c++11中关于std::thread的join详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
关于C#中Thread.Join()的一点理解
是第一次在C#中接触Thread,自己研究了一下其中Thread.Join()这个方法,下面谈谈自己的理解。 Thread.Join()在MSDN中的解释很模糊:Blocks the calling thread until a thread terminates 有两个主要问题: 1.什么是the calling thread? 2.什么是a thread? 首先来看一下有关的概念: 我们执行一个.exe文件实际上是开启了一个进程,同时开启了至少一个线程, 但是真正干活的是线程,好比一个Team有好几个人,但是真正干活的是人不是Team. 具体到代
C#多线程之Thread中Thread.Join()函数用法分析
主要介绍了C#多线程之Thread中Thread.Join()函数用法,实例分析了Thread.Join()方法的原理与使用技巧,非常具有实用价值,需要的朋友可以参考下
Java中Thread.join()的使用方法
主要介绍了Java中Thread.join()的使用方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
java 中Thread.join()的使用方法
主要介绍了java 中Thread.join()的使用方法的相关资料,需要的朋友可以参考下


