user2261403
user2261403

Reputation:

How to get the variables out of a new thread?

How can i get the variables out of a new thread created with:

public class ParseJson 
{
    public static String parsejson(String strHttpGet)
    {
        Thread thread = new Thread(new Runnable()
        {
            public String run(String strHttpGet)
            {
                String decodeJson = "someJson";
                return decodeJson;
            }
        });
            thread.start();
    }
}

I mean how can i get back the decoded json to my function parseJson and give it back to my function call String decodedJson = ParseJson.parseJson(strHttpGet);?

Upvotes: 0

Views: 180

Answers (4)

Xavi López
Xavi López

Reputation: 27880

You can't return a value from a Thread in Java. Actually, run() doesn't have a return type.

You could use a shared custom Object that will hold the result. Declare it as final, so you can access it in the anonymous subclass (that would be equivalent to passing a reference to the subclass), and just call a setter on it when the work is done.

public class ParseJson {
    public static String parsejson(final String strHttpGet) {
        final StringHolder ob = new MyObject();
        Thread thread = new Thread() {
            public String run() {
                String decodeJson = "someJson";
                ob.setResult(decodeJson);
            }
        };
        thread.start();
    }
    private static class StringHolder (){
        private String result;
        public String getResult() { return result; }
        public void setResult(String r) { result = r; }
    }
}

I'm not sure I understood why you said get back the decoded json to my function parseJson and give it back to my function call. Do you mean you'll just wait in that function until the Thread is finished? If that's what you want (again, why start a Thread?), you could use Thread.join().

Although if you want to get notified when the Thread finishes, you should indeed look into another option. Neil and Waqas have given good approaches.

Upvotes: 1

jontejj
jontejj

Reputation: 2840

You could use a Future

public class Json {
    private static final ExecutorService executor = Executors.newCachedThreadPool();

    public static String parse(final String strHttpGet)
            throws TimeoutException, InterruptedException, ExecutionException {
        Future<String> jsonTask = executor.submit(new Callable<String>() {
                    @Override
                    public String call() throws Exception {
                        String decodeJson = decodeJson(strHttpGet);
                        return decodeJson;
                    }

                    private String decodeJson(String strHttpGet) {
                        // TODO do actual parsing
                        return null;
                    }
                });
        // Allow the parsing to take one second
        return jsonTask.get(1, TimeUnit.SECONDS);
    }
}

Upvotes: 0

Neil Townsend
Neil Townsend

Reputation: 6084

In android, which you have tagged this question as, it would be simpler to use AsyncTask for this situation, because when you override the onPostEXecute method you can put in all sorts of things:

Upvotes: 1

waqaslam
waqaslam

Reputation: 68177

You may even use an Observer/Observable pattern for this.

Upvotes: 0

Related Questions