multithreading - In java how can I get a return value from a thread? -
the following code compiles results in null pointer exception @ run time. best guess outputints not "visible" each thread , therefore cannot written to.
public class myclass{ arraylist<int> outputints public void foo(int start, int end) { (int = start; <= end; i++) { bar(i); } private void bar(int i) { class oneshottask implements runnable { int i; oneshottask(int j) { = j; } @override public void run() { try { system.out.println(i); outputints.add(i); //null pointer exception caused here } } catch (exception e) { system.out.println(e.tostring()); } } } thread t = new thread(new oneshottask(j)); t.start(); } }
i've read should using callable achieve don't understand how implemented. examples java runnable run() method returning value seem suggest can run handful of threads using callable may need somewhere in region of thousand threads.
i'm looking in getting either above implementation work or layman's terms guide how can achieved callable
arraylist<integer> outputints = new arraylist<integer>();
your ide should shield kind of errors...
Comments
Post a Comment