Solved

Threads & CPU usage

Posted on 2003-11-11
13
735 Views
Last Modified: 2010-03-31
Hi!

My OS is Windows 2000. I have noticed that when I run threads the CPU usage goes to 100%. I was wondering if there is a way of limiting how much CPU can my threads use, thus leaving some for other activities.

Many thanks
Bahman
0
Comment
Question by:Bahman
  • 4
  • 4
  • 3
  • +1
13 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 9720866
Any application 'runs threads'. It's what those threads are doing that counts. There are many reasons that they could take up a lot of CPU, including programming errors. If there are no errors, lowering the thread priority may help.
0
 
LVL 7

Expert Comment

by:grim_toaster
ID: 9721350
I would suggest that you run perfmon (as you're using a windows machine) to find out exactly what thread/s are taking up all of your CPU, the do a thread dump of your application to find the corresponding thread id, and find out what code is causing the problems.  Recently I discovered one on an application that was simply an infinite loop (i.e. while (true) { }) and removing was a HUGE benefit.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 9721363
>>Recently I discovered one on an application that was simply an infinite loop

That would come into the category of errors, i'd think ;-)
0
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
LVL 35

Expert Comment

by:TimYates
ID: 9721607
> I have noticed that when I run threads the CPU usage goes to 100%

Does it actually stop running anything else?

If it is just running the java app, then it *should* use 100%, as nothing else is running, and so it will devote all processor usage to the only running application...Java...
0
 
LVL 35

Expert Comment

by:TimYates
ID: 9721608
> Does it actually stop running anything else?

I mean "Does it refuse to do anything else (lock up)?"
0
 
LVL 7

Expert Comment

by:grim_toaster
ID: 9721609
Yep!  But you should've seen some of the other errors!
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 9721629
>>then it *should* use 100%,

really? A machine devoid of other running programs and processes would be a strange beast indeed anyway..;-)
0
 
LVL 35

Expert Comment

by:TimYates
ID: 9721666
> A machine devoid of other running programs and processes would be a strange beast indeed anyway..;-)

Heheheh, true...  But many a non-bug has been found by studying the processor and memory usage graphs on Windows...

;-)
0
 

Author Comment

by:Bahman
ID: 9730343
Thank yoou for all the comments,

I have checked and checked again and there is no error, however there are while loops with (true) statement which are killed (removed, break out of loop..) in their due time.

When running the application the cpu shows 100% and it should since there is no other application to run, BUT if I want to run/open another application, it takes time i.e slower...

As I undrestand from your comments, the 100% CPU capacity is divided into the number of threads running, so if I run 1 thread and try to start another application then the CPU usage is divided into 2, i.e. 50% 50% (considering the priority I suppose) but if I run 39 threads and try to open another application then 100/31 i.e. 3.2% is given to the last application.  Am I correct?

If so, it would have been intersting to be able to say to my first application (regardless of number of threads it is running) "You shall have only 60% of the cpu usage if you are alone or not", in this way leaving the 40% for other applications.....


Many thanks

Bahman
0
 
LVL 35

Expert Comment

by:TimYates
ID: 9730372
As CEHJ said, try lowering the priority of the threads...

thread.setPriority( Thread.MIN_PRIORITY + 1 ) ;
0
 
LVL 7

Accepted Solution

by:
grim_toaster earned 20 total points
ID: 9730470
Or as you are running under windows, you can lower the priority of the java instance.  Go to task manager. and the resources tab, select the java instance, right click, set priority, and select low.

Also, did you run it through perfmon?
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 9730556
>>If so, it would have been intersting to be able to say to my first application...

Unfortunately that's not how threads work in Windows. That model is a time-sliced one, whereas Windows works in a pre-emptive way: other threads can take over the CPU based on their priority.

Interesting one grim_toaster - wasn't aware you could nice a process in Windows.
0
 

Author Comment

by:Bahman
ID: 9730731
Thank you for your comments.

>> Also, did you run it through perfmon?

All the threads I am running do the same thing, so there is no "bad_apple" if one is bad the all are bad :)

Now, I like to express my thanks to CEHJ, TimYates, and frim_toaster. I like to accept all the comments but I can only do accpet one :(

Best Regards

Bahman
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
jmss example java 2 23
Is there a simpler dropbox system? 10 34
running on tomcat not jboss eap 7.0 3 17
jsp error 6 25
For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question