linux top show java threads? - java

In a linux server(fedora), we run a single JBOSS Application Server, and we use quartz to schedule our task.
Yesterday,
I use a top command to view process status
see more than one processes named Java with different pid are displayed.
But if i use ps aux|grep java only one java process(Jboss AS) is displayed?
So my question would be:
Is a java thread mapped to native linux thread(cloned process), or does top not show threads?
Env:
Kernerl: 2.6.18
JDK: 1.6.0-23

It depends on your versions of various tools. Most likely, you have a version of top that doesn't understand the relationship between threads and processes on Linux, resulting in it incorrectly showing each thread as its own process. The implementation of Java threads depends on your VM, but the typical implementation on modern JVMs and Linux versions is 1-to-1, that is, each Java thread is a Linux KSE created by the clone system call.
If you have a Linux distribution that came out within the past three years, you shouldn't have this issue.

Related

how to start jvm and can we have multiple jvm running on a single system?

I had been asked this question in interview, how to start jvm and can we have multiple jvm running on a single system?
Each Java application running uses an independent JVM.
Each JVM is a separate process, and they do not share stacks, heaps. Only common core JVM and native libraries will be shared.
You can have n number of application running on single machine/server box, and so as n number of JVM's.
Launching multiple java processes will create JVM for you.
In all you can have any number of JVM running in your machine, with even different JDK versions.
How to start jvm.
If you have a Java JRE or JDK, then the simple way to start a JVM is to run the java command. For example:
java -jar someapp.jar
starts a JVM that runs the application in the supplied ("executable") JAR file.
Can we have multiple jvm running on a single system?
Yes. Provided you have enough memory.
On a typical OS, each JVM runs as a process. Assuming that the OS allows you to launch multiple processes, you can run multiple JVMs. (This is certainly true for Windows, Linux, MacOSX and other varieties of UNIX on which Java runs.)
The way to start a jvm is by invoking the main, either by invoking a jar using java -jar MyJar or by simply running main class from an IDE.
Yes, Multiple jvm instances can be run on a single machine, they all will have their own memory allocated.
There will be that many jvms as many main programs you run.

Java project shows different number of processes under windows and linux

I wrote a client server java project using Netbeans on Windows platform. When I profile it on windows it shows 8 threads running but when I run it on Ubuntu, htop shows 21 processes(threads) attached to it. Does anyone know why?
Yes. The numbers mean different things. In Windows there are process and threads. In contrast, Linux uses a 1-1 threading model.

Run Java Threads On One CPU

We Have a Multi-threaded Application in JAVA which has multiple threads running in parallel. Now we want to run all these threads on a single core. Currently application is running on a system having more then one Cores.
We know there is a technique available ProcesAffinity in .Net Framework to set process affinity.
But we don't want to depend on .Net Framework, because our application is build in java.
Do we set Process affinity using Bat file and run our application executable jar file through Bat file?
Currently our application is running on Window XP. So we need a solution that should be working fine on XP platform.
EDIT:
It's possible: See Java thread affinity
Pure Java doesn't support running a thread on specific processor. Check the SO question linked above.
Personally, I don't think that the fact that this cannot be set in pure Java is a bad thing, as to me, how an app is run does very much depend on the OS, so therefore a OS-specific solution isn't a bad thing.
You can use the MS psexec utility to set the affinity:
psexec -a 1 java -jar myapplication.jar
Would instruct that all of the threads created by java would be run on the lowest CPU.
And this line would be your .BAT file...
You cannot do it in pure Java. But on some versions of Windows, you can do it via operating system utilities; see https://superuser.com/questions/309617/how-to-limit-a-process-to-a-single-cpu-core ... and you might be able to do this by calling native libraries via JNI.

How java threads are scheduled?

I have recently started multi-threaded programming with Java
in case of Linux threads , i know that the kernel schedules them(as they are the unit entities that are scheduled)but java programs are run on JVM which in my system (RHEL 6.1) is implemented as a program that is run as a user space instance .So, without the kernel being aware of the java threads, how come preemptive multitasking is done in JVM? it will be helpful if the whole mechanism of JVM and kernel interaction in doing this thing is given.
Please cite possible sources of info
Threads in the java/JVM process maps to a native thread and you can see both the java thread id and the native thread id in a thread stack trace dump. Get the thread stack of all java threads using your favorite tool:
command line signal like ctrl+break (windows) or ctrl+\ linux) in the console where the java program is running
command line tool (kill -QUIT or jstack from the jdk)
visual vm in the jdk and/or jmx etc
Example extract from the first line of such a thread dump:
... tid=0x0000002adaba9c00 nid=0x754c ...
tid = java thread id
nid = native id (the OS thread id)
Use the operating system's tools to find out more about the thread using the native id (it is in hex).
Inside the java code you have ThreadMXBean to retrieve more thread information programatically if you want
http://docs.oracle.com/javase/6/docs/api/java/lang/management/ThreadMXBean.html
Read Distinguish Java threads and OS threads?
As I said in the comment Java threads are ordinary OS threads just running JVM code
The jvm is just a normal process which starts with one thread and can spawn as much threads it likes afterwards. Scheduling is done on two levels - between processes and between threads inside processes. All this is done by the OS (via libs) - the jvm just hooks in. Google posix threads for more details - thats whats exposed (API) to the jvm.
This goes a bit into details:
http://www.ibm.com/developerworks/java/library/j-rtj3/
"but java programs are run on JVM which in my system (RHEL 6.1) is
implemented as a program that is run as a user space instance.So,
without the kernel being aware of the java threads ..."
This statement is incorrect for all modern JVM's that use native threads. I think thats been the default since Java 1.2.
Native Thread implementation by a JVM means that each time a thread instantiates/runs a thread in Java code, the JVM asks the OS to create the thread. Since these are native threads, the kernel knows about them and treats them accordingly. Furthermore, Linux supports/implements POSIX threads, and as such on a Linux based systems you will get pthread behavior for your Java apps' threads

Are there any Java libraries for adjusting IO scheduler priority on linux

I have a Java program running on a linux system, which I would like to give a higher IO Scheduler priority. Is there a library JNI/JNA based that would allow me to do that from within my code?
You could spawn and external command and call ionice on your process.
You can get the PID of the Java Process by using the Java Management API, at least when using the Sun JVM. Have a look at the actual implementations of the management objects, one returns the pid (don't know which for the moment).
Then I would use jnative to call the linux function, so you don't have to rely on ionice to be installed.

Categories

Resources