I'm stymied- can someone suggest how I can correctly pass a TreeMap (or similar collection) parameter to a thread. Using a named class, I can pass a String parameter no problem, but after much experimentation can't get TreeMap to work - it always has zero length. Obviously a String is very different from a TreeMap but still there should be some way to make it work.
The program is a background service that gets data from various sensors - I need to do some data processing, save to sd card etc. on a separate thread. I've read numerous posts such as Runnable with a parameter? and various duplicates without finding any solutions.
// String version works fine
public class MyThread implements Runnable {
private String s;
public MyThread(String s) {
this.s = s;
}
public void run() {
// works great, can process s data, save to sd card, etc.
}
}
//called by
String s = "mylongcsvstring";
Runnable r = new MyThread(s);
new Thread(r).start();
//TreeMap version doesn't work
public class MyThread implements Runnable {
private TreeMap<Long, SomeObject> t;
public MyThread(TreeMap<Long, SomeObject> t) {
this.t = t;
}
public void run() {
// need to process t but size is zero
}
}
//called by
TreeMap<Long, SomeObject> tm = new TreeMap<>();
tm.put(Long,SomeObject); // up to a few hundred objects
Runnable r = new MyThread(tm);
new Thread(r).start();