Hi, If you fork you have to use reconnect_JVM, or else you will run into trouble eventually. What will happen is that all the processes will use the same socket connection the JVM, and eventually things will get messed up.
However, Java objects created from a Perl process can't be accessed directly by other processes using Inline::Java. This is meant to be a minimal security mechanism when using SHARED_JVM mode. It helps keep each Perl processe's Java objects somewhat separate. If you really want to keep your current design, you will have to keep your objects on the Java side in an array and access them indirectly. That way the objects references remain in Java-land and can re-fetched by the different threads. Here's an example: use Inline ( Java => 'DATA', SHARED_JVM => 1, PORT => 17891, ) ; Inline::Java::capture_JVM() ; # Kill JVM on exit my $nb = $ARGV[0] || 5 ; for (my $i = 0 ; $i < $nb ; $i++){ test->make_test($i) ; } $starttime = time; for (my $i = 0 ; $i < $nb ; $i++){ my $pid = fork; if (! $pid) { print "start $i\n"; Inline::Java::reconnect_JVM() ; my $test = test->get_test($i) ; $test->sleep() ; print "end $i\n"; exit; } else { push @pid, $pid; } } waitpid $_,0 for @pid; $elapsed = time-$starttime; print "For $nb threads it took $elapsed seconds\n"; __DATA__ __Java__ import java.util.* ; class test { static ArrayList tests ; static { tests = new ArrayList() ; } static public void make_test (int i) { tests.add(i, new test()) ; } static public test get_test (int i) { return (test)tests.get(i) ; } private test() { } public void sleep() throws InterruptedException { Thread.sleep(1000) ; } } Patrick On Mon, Jan 3, 2011 at 3:59 PM, ant <a...@suave.net> wrote: > > > I made this work by turning off SHARED_JVM (because after a while it seemed > to cause problems) and then loading the module that had Inline::Java in it > at runtime in an eval instead because reconnect_JVM didn't seem to work > right at all. > > so basically I did > fork(); > eval "use My::Module;"; > > I'll be interested to see if you get a better solution. > > On Mon, 3 Jan 2011 12:53:37 -0600, <scott.dav...@wellsfargo.com> wrote: >> Hi Patrick, >> >> Thanks for writing back! I am calling from Perl to Java only. Here is a >> closer example to what I am doing. I want to create several objects in > the >> main process, and then execute methods in parallel in sub-processes. > There >> are several steps to what I am doing, and I want to ensure that the first >> step completes on all target servers before moving on to the second step, >> and so forth. >> >> use Inline ( >> Java => 'DATA', >> SHARED_JVM => 1, >> PORT => 17891, >> ) ; >> Inline::Java::capture_JVM() ; # Kill JVM on exit >> >> my $nb = $ARGV[0] || 5 ; >> for (my $i = 0 ; $i < $nb ; $i++){ >> $test[$i] = new test; >> } >> >> $starttime = time; >> for (my $i = 0 ; $i < $nb ; $i++){ >> my $pid = fork; >> if (! $pid) >> { >> print "start $i\n"; >> #Inline::Java::reconnect_JVM() ; >> $test[$i]->sleep(); >> print "end $i\n"; >> exit; >> } >> else >> { >> push @pid, $pid; >> } >> } >> >> waitpid $_,0 for @pid; >> >> $elapsed = time-$starttime; >> print "For $nb threads it took $elapsed seconds\n"; >> >> __DATA__ >> __Java__ >> class test { >> public test() { } >> public void sleep() throws InterruptedException { >> Thread.sleep(1000) ; >> } >> } >> >> --------------- >> >> $ perl ptest3.pl 2 >> start 0 >> start 1 >> end 0 >> end 1 >> For 2 threads it took 2 seconds >> $ perl ptest3.pl 5 >> start 0 >> start 1 >> start 2 >> start 3 >> start 4 >> end 0 >> end 1 >> end 2 >> end 4 >> end 3 >> For 5 threads it took 5 seconds >> $ perl ptest3.pl 10 >> start 0 >> start 1 >> start 2 >> start 3 >> start 4 >> start 5 >> start 6 >> start 7 >> start 8 >> start 9 >> end 0 >> end 3 >> end 6 >> end 1 >> end 8 >> end 9 >> end 5 >> end 7 >> end 2 >> end 4 >> For 10 threads it took 10 seconds >> >> Note that when I uncomment the reconnect_JVM() line, then I get an error >> message: >> $ perl ptest3.pl 2 >> start 0 >> start 1 >> Can't find object 1 for thread IJST-#1 at (eval 13) line 215 >> at ptest3.pl line 24 >> Can't find object 2 for thread IJST-#2 at (eval 13) line 215 >> at ptest3.pl line 24 >> For 2 threads it took 0 seconds >> >> Thanks, >> Scott > -- ===================== Patrick LeBoutillier Rosemère, Québec, Canada