Java - C-Like Fork? - java

Is it possible to do a "C like" fork in java, using an new independent jvm process ?
How?

This answer is probably a little late but:
http://akuma.kohsuke.org/
seems to be exactly what your looking for

Funnily, I am just working on this: a Java process running other Java processes. I used the article From Runtime.exec() to ProcessBuilder as a solid base, and When Runtime.exec() won't as a good advice how to gobble the output streams.
PS.: For those wondering, I had to do that (instead of spawning new threads) because yet another Java process is checking the presence of these processes which are, normally, ran separately with shell commands.

The Application Isolation API (JSR 121) introduces Isolate which addresses this use case.

Related

Is there an alternative to Runtime.getRuntime().exec()

Just wondering, if there is something better, newer, safer, faster, etc than Runtime.getRuntime().exec().
I want to run another process from my application on linux, and this is the only way i know how. Would be nice to have an alternative.
How about ProcessBuilder?
A bit more:
Introduced in Java 1.5, allows you to gain more control on the process environment - set the working directory, let you redirect the error stream to the input stream (from java POV) and a few more things.
From Oracle's site:
ProcessBuilder - The new ProcessBuilder class provides a more
convenient way to invoke subprocesses than does Runtime.exec. In
particular, ProcessBuilder makes it easy to start a subprocess with a
modified process environment (that is, one based on the parent's
process environment, but with a few changes).

Achieving multi-core in Java - how?

What is the easiest way in Java for achieving multi-core? And by this, I mean, to specifically point out on what core to execute some parts of the project, so good-old "normal" java threads are not an option.
So far, I was suggested JConqurr (which is an Eclipse toolkit for multi-core programming in java), JaMP (which extends Java for OpenMP), and MPJ express, of which I don't know much. Which of the above do you consider the best, or do you have other suggestions? It would be preferable to somehow mesure the performance boost/gain, but not exclusive.
Any help would be much appreciated.
Thanks,
twentynine.
Even though it is easy to write multi-threaded code in Java, there is nothing in the Java standard runtime which generically allow you to tell the JVM or the operating system how to schedule your program.
Hence you will need to have code specifically for your JVM and/or your operating system, and that code may not be doable in Java (unless you dive into JNI or JNA). External programs can pin processes to a CPU in many Unix versions (and probably Windows too), but I don't think you can do this for individual threads.
Scala is quite popular for this. It runs on the JVM and has bindings for Java to hook them together.

Sending a POSIX signal from the JVM

How do I send a POSIX signal from within the JVM? (in Java or Clojure)
I never thought this would be an issue until I tried googling it — there is lots of information about handling signals, but nothing about sending them.
Short of using the JNI or calling the shell to execute "kill", is there any other way to send a signal to a PID?
Ok. Answering myself: I looked at the suggested libraries, but I am wary of introducing new dependencies on lots of code, especially if I'll only use a small part of it.
It turns out the easiest way is to use JNA and interface with the system (libc) API. In clojure this pretty much amounts to:
(jna-invoke Integer kill pid signo)
after doing a (:use net.n01se.clojure-jna) of course.
Since this software is not intended to ever run on Windows, I'm fine with this solution which should work on all POSIX systems (MacOS and Linux are what I'm interested in).
POSIX signals is OS specific functions, but JVM is tried to be OS independent VM. So there are no standard functions for this.
But you can execute shell commands (using Runtime class) or use some lbrary for your needs, like a Posix for Java

How to find CPU-intensive class in Java?

I have a big program in Java that uses multithreading. In some cases, the program starts using 100% of three cores of my eight core system. In normal use, the program use all cores at 1-2%. How can I find the class that's overloading cores?
Use a profiler such as the jvisualvm that is bundled with jdk-1.6.0_10
The best solution is to use a profiler - that's what they're built for, and there's a great one bundled with Java 6.
Another (far from being as ideal a solution) is to run your program in the Eclipse IDE (if that's what you use) in debug mode. You can then look at the running threads. IF a lot of them are suspended, the one that is not might be your culprit. Force it to break (from the toolbar) and you can see where it is. There are many chances that you'll find a clear loop or busy waiting.
Use a profiler to figure out which thread(s) are using all of your CPU cycles, and the method(s) they are executing.
If you are using Eclipse, you can use the TPTP profiling tool.
If you are going the commercial profiler route then I would recommend using Dynatrace.
Try taking threads dump (see jps, jstack commands) and then see which methods are executed.
If you are using Java over UNIX or some versions of Linux look into DTrace with Java.

Writing functional programs in non-functional languages

Suppose I write a program using immutable data structures in Java. Even though it is not a functional language, it should be able to execute parallely. How do I ensure that my program is being executed using all the cores of my processer? How does the computer decide which code can be run parallely?
P.S. My intent in asking this question was not to find out how to parrallelize java programs. But to know - how does the computer parallelize code. Can it do it in a functional program written in a non functional language?
Java programs are parallelized through threads. The computer can't magically figure out how to distribute the pieces of your application across all the cores in an imperative language like Java. Only a functional language like Erlang or Haskell could do that. Read up on Java threads.
I am not aware of automatic parallelization JVMs. They do exist for other languages such as FORTRAN.
You might find the JSR166y fork-join framework scheduled for JDK7 interesting.
i dont think you can "force" the JVM to parallelize your program, but having a separate thread executing each "task", if you can break down your program that way, would probably do the trick in most cases? parallelism is still not guaranteed however.
You can write functions with automatically parallelise tasks, it is fairly easy to do for specific cases, however I am not aware of any built-in Java API which does this. (Except perhaps the Executor/ExecutorService)
Something that I used in school that did alot of the work for you.
http://www.cs.rit.edu/~ark/pj.shtml
It has the capability to do SMP or message based parallelism.
Whether or not it is useful to you is a different question :-)

Categories

Resources