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?
Distributing words among two processes
Page 1 of 18 Replies - 193 Views - Last Post: 31 January 2012 - 08:03 PM
Replies To: Distributing words among two processes
#2
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.
You have given some really vague information so I cannot give you the best route to take just a general solution.
#3
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.
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.
#4
Re: Distributing words among two processes
Posted 31 January 2012 - 02:32 PM
Use ArrayList.subList.
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.
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.
#5
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.
#6
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
#7
Re: Distributing words among two processes
Posted 31 January 2012 - 06:37 PM
I have gotten this far now:
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 ?
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
#8
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
#9
Re: Distributing words among two processes
Posted 31 January 2012 - 08:03 PM
thanks man
Page 1 of 1
|
|

New Topic/Question
Reply



MultiQuote






|