在一个类A中有个线程内部类,用于读取服务器返回的字符串数据,然后在A中写了一个接收线程中传过来的字符串参数,然后供A类中的其他方法的某些地方引用。
如下:
class A{
private String mess="";
class ReceivedThread extends Thread{
//读取服务器数据,返回一条字符串,比如str
r(str)
}
private String r(String s){
this.mess = s;
return mess;
}
f(){
new ReceivedThread().start();
//接下来将引用mess变量,为啥此时的mess值还是为空呢?线程也执行了,r()也返回了有值的字符串。求指点
}
}
如下:
class A{
private String mess="";
class ReceivedThread extends Thread{
//读取服务器数据,返回一条字符串,比如str
r(str)
}
private String r(String s){
this.mess = s;
return mess;
}
f(){
new ReceivedThread().start();
//接下来将引用mess变量,为啥此时的mess值还是为空呢?线程也执行了,r()也返回了有值的字符串。求指点
}
}
this.mess = str;
System.out.println("3");
return mess;
}
class ReceivedThread extends Thread{
long time;
public ReceivedThread(long time) {
this.time = time;
}
@Override
public void run() {
synchronized (in) {
final char temp[] = new char[200];
while(!Thread.interrupted()){
try {
if(stream.available() > 0){
in.read(temp);
mess = String.valueOf(temp).trim();
System.out.println("thread_mess-----"+mess);
System.out.println("2");
if(!TextUtils.isEmpty(mess)){
receivedData(mess);
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
this.interrupt();//中断线程
}
}
};某方法里片段代码
if(sb.lastIndexOf("phonelogin") >= 0){
new ReceivedThread(143).start();
//启动线程后,下面的代码要引用mess,为空的
if(!TextUtils.isEmpty(mess) && mess.contains("phoneloginreceived")){
System.out.println("data---------"+this.mess);
}else{
sendBroadcast(new Intent("com.jillion.phone.SOCKET_LOGIN"));
Toast.makeText(SocketService.this, "无法连接到服务器0000", Toast.LENGTH_SHORT).show();
}
}