java - How do I return a String from a Thread or get a string from a Thread? -


i need string value thread, don't it! global variables don't work! can me please?

this code. need use datastring:

public class deserializable {  public void execute() {      new thread() {          public void run() {             string surl = "http://myaddressxxxxx";             httpurlconnection urlconnection = null;             url url = null;             try {                 url = new url(surl);                  urlconnection = (httpurlconnection) url.openconnection();                 inputstream in = new bufferedinputstream(                         urlconnection.getinputstream());                 int b = in.read();                 list<byte> bytes = new linkedlist<byte>();                 while (b != -1) {                     bytes.add((byte) b);                     b = in.read();                 }                 byte[] array = new byte[bytes.size()];                 (int = 0; < bytes.size(); i++) {                     array[i] = bytes.get(i).bytevalue();                 }                  // need return string.                 string datastring = new string(array);              } catch (exception e) {                 e.printstacktrace();             } {                 urlconnection.disconnect();             }         }     }.start();  } 

thread can extended have fields, datastring. these fields can accessed same thread or different one, long have references each other.

public class threada extends thread {    public string datastring;     public void run(){       ...       this.datastring = ...;       ...    } }  public class threadb extends thread {     private final threada ta;     public threadb(threada ta){       super();       this.ta = ta;    }     public void run(){       ...       ta.datastring...       ...    } } 

of course, poses problem of concurrent access field datastring. consider using synchronized, if issue in case. have @ tutorial on concurrency more information.


Comments

Popular posts from this blog

c# - Better 64-bit byte array hash -

webrtc - Which ICE candidate am I using and why? -

php - Zend Framework / Skeleton-Application / Composer install issue -