Solved

Threads & CPU usage

Posted on 2003-11-11
13
719 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
 
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
Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

 
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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
This video teaches viewers about errors in exception handling.

760 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now