java - How to preserve and object instantiated in a thread -
supposing have following
public class foo { private map<integer,someobject> mymap; public foo() { this.mymap = new hashmap<integer,someobject>(); } private class runner implements runnable { public void run() { someobject someobj = new someobject(); foo.this.mymap.put(10,someobj); //'soobj' null upon retrieval later... } } }
if create thread runnable of type 'runner' , start thread, run. in run method, create instance of 'someobject' , place in map of outer class. however, when attempt value 'mymap' later on, 'someobject' instance null. can't understand why have placed reference map 'mymap' still lives on in heap after thread finishes. there way around this?!
thanks much
it problem way objects instantiated. should be:
foo foo = new foo(); thread thread = new thread(foo.new runner()); thread.start();
Comments
Post a Comment