4 Replies - 233 Views - Last Post: 26 July 2012 - 04:38 AM Rate Topic: -----

#1 paulSteve  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 25-July 12

How to get threads to take in data?

Posted 25 July 2012 - 04:20 PM

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?

Is This A Good Question/Topic? 0
  • +

Replies To: How to get threads to take in data?

#2 CasiOo  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1371
  • View blog
  • Posts: 3,022
  • Joined: 05-April 11

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();


Was This Post Helpful? 1
  • +
  • -

#3 Ghlavac  Icon User is offline

  • D.I.C Addict

Reputation: 84
  • View blog
  • Posts: 519
  • Joined: 14-January 09

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.
Was This Post Helpful? 0
  • +
  • -

#4 cfoley  Icon User is offline

  • Cabbage
  • member icon

Reputation: 1939
  • View blog
  • Posts: 4,027
  • Joined: 11-December 07

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.
Was This Post Helpful? 0
  • +
  • -

#5 CasiOo  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1371
  • View blog
  • Posts: 3,022
  • Joined: 05-April 11

Re: How to get threads to take in data?

Posted 26 July 2012 - 04:38 AM

View PostGhlavac, on 26 July 2012 - 09:25 AM, said:

Though you're gonna have to watch your synchronization.


Not if he is working on different maps
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1