I'm have and array of Maps and I want to create a thread for each map in the array and perform run() using the maps data. I have a class Run that implements runnable and I have an array of threads which all implement an instance of it. This was how I was taught to make threads and its worked so far. The problem I'm having is that each thread in the array needs to execution run() with a different map. I can't parameterize the class Run because then each Thread would have a different Run instance. How do I get the values needed to each thread?
How to get threads to take in data?
Page 1 of 14 Replies - 160 Views - Last Post: 26 July 2012 - 04:38 AM
Replies To: How to get threads to take in data?
#2
Re: How to get threads to take in data?
Posted 25 July 2012 - 05:42 PM
Simply give them a map in their constructor 
public class MyThread implements Runnable {
private Map<Integer,Integer> map;
public void MyThread(Map<Integer,Integer> map) {
this.map = map;
}
public void run() {
}
}
new Thread(new MyThread(maps[i])).start();
#3
Re: How to get threads to take in data?
Posted 26 July 2012 - 02:25 AM
Though you're gonna have to watch your synchronization.
#4
Re: How to get threads to take in data?
Posted 26 July 2012 - 03:21 AM
Quote
I can't parameterize the class Run because then each Thread would have a different Run instance.
Exactly! You do need a separate instance of Run for each thread. Incidentally, Run is a confusing name since there is a Runnable interface with a run() method. Why not call it MapProcessor or something even more descriptive.
#5
Re: How to get threads to take in data?
Posted 26 July 2012 - 04:38 AM
Page 1 of 1
|
|

New Topic/Question
Reply



MultiQuote





|