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
java android multithreading runnable


source to share


No one has answered this question yet

See similar questions:

121
Runnable with parameter?

or similar:

6170
Is Java "pass-by-reference" or "pass-by-value"?
2097
Is there a way to run Python on Android?
1989
"implements Runnable" vs "extends Thread" in Java
1823
What's the easiest way to print a Java array?
1492
Does Java support default parameter values?
1270
How to pass data between activities in an Android app?
882
Static way to get "Context" in Android?
815
Android "Only the original thread that created the view hierarchy can touch its views."
761
What is daemon thread in Java?
696
Is there a way to kill the thread?



All Articles
Loading...
X
Show
Funny
Dev
Pics