[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

Threads & CPU usage

Posted on 2003-11-11
13
Medium Priority
?
758 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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
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 80 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

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

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

By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
Suggested Courses

650 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