怎么用用线程中Runnable里面的东西,求解释
android开发吧
全部回复
仅看楼主
level 9
莫荩堇 楼主
我在一个类中开启了一个线程,线程是用Runnable写的
我现在new Thread(Runnable)
然后我想调用Runnable里面的东西,用怎么做?
比如说,我在Runnable里面写了个连接Socket,我监听端口
现在我要在开启线程的地方判断Socket是否还在连接
怎么办?
2013年10月26日 04点10分 1
level 9
莫荩堇 楼主
public class Socket_Thread implements Runnable{
String IP_str,liaotian;
int duankou_str;
BufferedReader Buffer_is;
OutputStream os;
Socket socket;
//构造方法,取得IP地址,端口号,显示面板
public Socket_Thread(String iP_str, int duankou_str,TextView show_xinxi) {
this.IP_str=iP_str;
this.duankou_str=duankou_str;
}
@Override
public void run() {
try {
socket=new Socket(IP_str,duankou_str);
socket.setSoTimeout(5000);
//取得socket的输入流
Buffer_is = new BufferedReader(new InputStreamReader(socket.getInputStream()));
//取得socket的输出流
os = socket.getOutputStream();
//测试连接
os.write("连接成功。。。。\n".getBytes("GBK"));
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
while (true) {
try {
liaotian+=("\n"+Buffer_is.readLine());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
2013年10月26日 04点10分 2
level 9
莫荩堇 楼主
socket_thread=new Socket_Thread
(IP_str,duankou_int,xianshi);
thread=new Thread(socket_thread);
thread.start();
if(socket_thread.socket.isConnected()){
System.out.println("已经连接")
}
就是这样子,我要在另一个Activity判断是否连接
2013年10月26日 04点10分 3
1