Find the method for obtaining the Java thread ID in Linux

Source: Internet
Author: User

Most of the information provided on the Internet is provided to find the thread ID, which is limited to the ID of the thread object inside the Java program, rather than the thread ID of the entire system, it was originally expected that the thread id = process ID + internal thread object ID, but the experiment results show that this formula is not true, later, the senior engineer said that there is a one-to-one correspondence between the internal thread object ID and the thread ID in windows. In Linux, this relationship is not fixed, so let's use another method.

Finally, find a thread ID that can get the Java program to execute the current task in Linux. The method is as follows:

1. Compile the system call C file to implement the gettid () method.

2. Use JNI to implement Java calls to C. You can get gettid () in Java and return the ID of the current thread.

Procedure

1. gettid. Java

Public class gettid {
Static
{
System. loadlibrary ("gettid ");
}
Public native int gettid (); // local method declaration
Public int getthreadid ()
{
Return gettid ();
}
}

--------------------------------------------------------------------------

2. Compile this Java file using JNI to generate gettid. H (For details, refer to my other blog post-JNI call)

3. gettid. c

# Include <sys/syscall. h>
# Include <sys/types. h>
# Include <getopt. h>
# Include <string. h>
# Include <stdlib. h>
# Include <errno. h>
# Include <stdio. h>
# Include <unistd. h>
# Include <gettid. h>

# Define syscall_name "gettid"

# Ifndef _ nr_gettid
# DEFINE _ nr_gettid 224
# Endif

Jniexport int jnicall java_gettid_gettid
(Jnienv * ENV, jobject OBJ)
{
Int sys_ret;
Sys_ret = (pid_t) syscall (_ nr_gettid); // use the system call to obtain tid. Although the man gettid () function is used in Linux terminals, but the specific implementation must be done by yourself.
Printf ("% d", sys_ret );
Return sys_ret;
}

--------------------------------------------------------------------------

4. makefile

Libgettid. So: gettid. o
Gcc-wall-rdynamic-shared-O libgettid. So gettid. o
Gettid. O: gettid. c gettid. h
Gcc-wall-C gettid. c-I. /-I/usr/include-I/home/TINA/workspace/tenantmanager/src/aspect-I/home/TINA/jdk1.6.0 _ 25/include-I/home/TINA/ jdk1.6.0 _ 25/include/Linux

---------------------------------------------------------------------------

5. compile and generate the. So file,

Copy the file to/usr/lib,

Then change the system environment variable file/etc/profile

Add export LD_LIBRARY_PATH =/home/usr/lib

After these steps, you can call the gettid () function in other Java programs.

Suggestion: I encountered many errors in the implementation process, some of which are about JNI and the others are about the compiling environment, we recommend that you use eclipse + CDT to compile C or Java in a Linux environment. Because of the dependency between files in complicated projects, javac or GCC alone cannot handle the dependency well, eclipse handles this well and you can borrow it.

Thank you for your help.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.