Solved

Threads & CPU usage

Posted on 2003-11-11
13
749 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

 
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

Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

Question has a verified solution.

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

Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
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…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

691 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