8 Replies - 333 Views - Last Post: 31 January 2012 - 08:03 PM Rate Topic: -----

#1 imu_1  Icon User is offline

  • D.I.C Regular

Reputation: -6
  • View blog
  • Posts: 256
  • Joined: 03-June 11

Distributing words among two processes

Posted 31 January 2012 - 01:27 PM

I have thousand words in a my arryaList.
I want to distribute them evenly to two processes so that they can process the words.

How can I approach this problem?
Is This A Good Question/Topic? 0
  • +

Replies To: Distributing words among two processes

#2 Fuzzyness  Icon User is offline

  • Comp Sci Student
  • member icon

Reputation: 669
  • View blog
  • Posts: 2,438
  • Joined: 06-March 09

Re: Distributing words among two processes

Posted 31 January 2012 - 01:42 PM

You could always split them into 2 arrays and have each process have its own array?

You have given some really vague information so I cannot give you the best route to take just a general solution.
Was This Post Helpful? 1
  • +
  • -

#3 imu_1  Icon User is offline

  • D.I.C Regular

Reputation: -6
  • View blog
  • Posts: 256
  • Joined: 03-June 11

Re: Distributing words among two processes

Posted 31 January 2012 - 02:00 PM

Suppose I have amethod called getArray() that returns the arrayList of thousand string words.
Runtime runtime = Runtime.getRuntime();
// Create a child process
Process process = runtime.exec("java subProcess");

OutputStream output = process.getOutputStream();
ObjectOutputStream obj = new ObjectOutputStream(output);
obj.flush();

obj.writeObject(getArray()); // sending the array to the child process for further processing
objos.flush();

As you can see, I am only creating one process and sending the entire doc to it. I would like to create two processes, split the doc into half and send the first half to the first process and second half to the second process.
Was This Post Helpful? 0
  • +
  • -

#4 blackcompe  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1155
  • View blog
  • Posts: 2,533
  • Joined: 05-May 05

Re: Distributing words among two processes

Posted 31 January 2012 - 02:32 PM

Use ArrayList.subList.

List list = getArrayList();
obj.writeObject(list.subList(list.size()/2, list.size())); 



If you're trying to improve performance by calling another process, consider using multi-threading, especially since you can't guarantee the 'java' command will execute on the user's machine.
Was This Post Helpful? 1
  • +
  • -

#5 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10558
  • View blog
  • Posts: 39,065
  • Joined: 27-December 08

Re: Distributing words among two processes

Posted 31 January 2012 - 02:39 PM

Just to be picky here: List list = getArrayList();, the List should be declared using generics to avoid a deprecation warning. :)
Was This Post Helpful? 1
  • +
  • -

#6 CasiOo  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1398
  • View blog
  • Posts: 3,094
  • Joined: 05-April 11

Re: Distributing words among two processes

Posted 31 January 2012 - 03:10 PM

Why dont you just start two threads ?? It would be much easier
Was This Post Helpful? 3
  • +
  • -

#7 imu_1  Icon User is offline

  • D.I.C Regular

Reputation: -6
  • View blog
  • Posts: 256
  • Joined: 03-June 11

Re: Distributing words among two processes

Posted 31 January 2012 - 06:37 PM

I have gotten this far now:
 
    			  List list = load.getFile(); 
			
	                   int begin = 0; 
			
			for(int i = 1; i <= num; i++)
			 { 
			    end = (list.size()/num) *i;
				
			    process[i] = rt.exec("java subProcess"); 
			// get an ObjectOutputStream for sending data to the child 
	         OutputStream os = process[i].getOutputStream();
	         ObjectOutputStream obj = new ObjectOutputStream(os);
             obj.flush();  // outputs the bytes to another outputstream 
               System.out.println("Parent sending: " + list.subList(begin, end));
				// send the ArrayList to the child
	           objos.writeObject(list.subList(begin, end)); 
			   obj.flush();
              begin = end+1; 
			  
			}



I am however getting this error:



C:\JAVA>java MultiProcess
Enter the number of processes : 2

Parent sending: [child, mango, orange, zebra, theory, tomato]
java.io.NotSerializableException: java.util.RandomAccessSubList
at java.io.ObjectOutputStream.writeObject0(Unknown Source)
at java.io.ObjectOutputStream.writeObject(Unknown Source)
at MultiProcess.main(MultiProcess.java:49)

It doesnt allow me to sned the data to the child process. Why ?

This post has been edited by imu_1: 31 January 2012 - 06:39 PM

Was This Post Helpful? 0
  • +
  • -

#8 blackcompe  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1155
  • View blog
  • Posts: 2,533
  • Joined: 05-May 05

Re: Distributing words among two processes

Posted 31 January 2012 - 06:52 PM

List.subList returns an implementation that's hidden from the client. Unfortunately, it's not serializable, meaning it can be sent through a stream. You need to use a serializable list.

ArrayList lst = new ArrayList(list.subList(begin, end));
objos.writeObject(lst); 


This post has been edited by blackcompe: 31 January 2012 - 06:53 PM

Was This Post Helpful? 1
  • +
  • -

#9 imu_1  Icon User is offline

  • D.I.C Regular

Reputation: -6
  • View blog
  • Posts: 256
  • Joined: 03-June 11

Re: Distributing words among two processes

Posted 31 January 2012 - 08:03 PM

thanks man
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1