Windows pull out Java program consumes high CPU thread and find the problem code dead loop thread code

Source: Internet
Author: User
Tags high cpu usage

One of my Java programs occasionally happens to have high CPU usage

I've never known why.

I finally took the time to solve it today.

The system is Win2003

JVISUALVM and Jconsole seem to see only a total CPU that doesn't see the CPU that each thread consumes separately.

So on the Windows platform to find out exactly which thread occupies the CPU is not so easy, Linux with top is much simpler

The final workaround:

1. Locate the PID corresponding to the Java process.

To find the PID method is: Open the Task Manager, and then click on the "View" menu, and then click "Select Column", the PID hook, then you can see in the task manager of all the process PID values. (can also be viewed directly with the tools mentioned in the third step)

2. Then export the Java process snapshot. Run the command directly.

[Java]View Plaincopy
    1. Jstack-l 31372 > c:/31372.stack


I am here to specify that all Java information is exported to the 31372.stack file of the C drive.

3. Under Windows, you can only view the CPU utilization of the process, to see the CPU utilization of the thread with other tools, I use the Microsoft-provided process Explorer v15.3

Http://technet.microsoft.com/en-us/sysinternals/bb896653.aspx

Unzip run after download

Right-click the process you want to see---properties

4. Then select the Threads tab to find the TID for the CPU-consuming thread, for example, I'm a 31876 thread here.

5. Convert the PID into 16, I am here directly with the system of the calculator conversion, placed on why to convert, because the previous information exported with Jstack in the thread corresponding to the TID is 16 binary.

Finally get the 16 binary value of the thread PID is 7c84

6. Find 7c84 in the 31372.stack file in C drive

Since my program is over, there is nothing unusual here, so there is nothing unusual here.

Before my question was resolved, I found the content here:

[Java]View Plaincopy
  1. "Thread-23" prio=6 tid=0x03072400 nid= 0x1b68runnable [0x0372f000]
  2. Java.lang.Thread.State:RUNNABLE
  3. At Com.horn.util.MyEncrypt.encode (Myencrypt.java:)
  4. At Com.horn.common.OrderUtil.hisExp (Orderutil.java:228)
  5. At COM.HORN.UTIL.MSGMANAGE.RECEIVEMSG (Msgmanage.java:961)
  6. At Com.horn.util.PollMessageThread.run (Pollmessagethread.java:)
  7. Locked ownable Synchronizers:
  8. -None


So open T Com.horn.util.MyEncrypt.encode (myencrypt.java:17)

After analyzing the code, the problem is found.

The problem code is:

[Java]View Plaincopy
    1. Random number of 100-999
    2. int random = (int) (Math.random () * 1000);
    3. while (Random < ) {
    4. Random = random * 10;
    5. }

So it's okay to see the tickets.
I didn't even notice when I wrote this code.
The key is that the value range of math.random () is greater than 0 and less than 1, right?
If the value of Math.random () is less than 0.00009 ... It becomes a cycle of death.

Now modify in order to

[Java]View Plaincopy
    1. Random number of 100-999
    2. int random = new Random (). Nextint (+ 100) ;


Hope to give some help to the friends who have trouble.

Windows pull out Java program consumes high CPU thread and find the problem code dead loop thread code

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.