Correct way to pass TreeMap parameter to Thread in Java on Android?

I'm confused - can someone tell me how I can properly pass the TreeMap parameter (or similar collection) to the stream. Using a named class, I can pass a String parameter without issue, but after a lot of experimentation I can't get TreeMap to work - it always has a length of zero. Obviously the string is very different from TreeMap, but still there must be some way to make it work.

The program is a background service that receives data from various sensors - I need to do some data processing, save to SD card, etc. in a separate topic. I've read numerous posts such as Runnable with parameter? and various duplicates without looking for 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();

      

+3


source to share





All Articles