aspose file tools
The moose likes Threads and Synchronization and the fly likes Threads - enough is enough Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Barcodes with iOS this week in the iOS forum
or Core Java for the Impatient in the Java 8 forum!

JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "Threads - enough is enough" Watch "Threads - enough is enough" New topic

Threads - enough is enough

Ben Roy
Ranch Hand

Joined: Nov 01, 2000
Posts: 70
I'm wondering if anyone can give me some insight into the overhead involved in creating and maintaining threads. How many is too many? I realize this is subjective to some degree, but I'm just trying to get a handle on what's practical or even generally accepted.
Rahul Mahindrakar
Ranch Hand

Joined: Jul 28, 2000
Posts: 1861
Hi Ben,
I have written some code. what it does is create a Runnable object whose run method just increments an int by 1. I have passed this Runnable object to 1000 threads. Thus the integer is incremented to 1000 in the end. I have noted down the time it takes to complete this operation. which is printed out.
In the second case i have invoked the run directly nearly 10000000 times which is 1000 times more that that of the number of theads created. In this case too i have noted the time. The results are printed out for you to see.
Satya Narayana

Joined: Nov 08, 2000
Posts: 9
Hi Rahul,
What this code will prove? I think something wrong in this program.
For one case you are just instantiating Thread object
In another case you are just calling run() method directly(without thread).
What we can conclude these two results one is time taken to create 100000 Thread objects( please remember invoking start method only causes creation of new thread)
and another is time taken to execute thread body without Threads.
Please check the program.
Here I'm posting your program with modifications. Please check these corrections are valid or not
It gives the comparative time results to execute a thread body with threads and without threads.

In my opinion this is not the bench mark to decide maximum number of threads. Just a guideline. This will vary with application. For tuning properly a multithreaded system, we need to test time calcultation using this type of opproach to minimize time per thread.
Thank you,

Please use [ code] [ /code] tags to format your code. Thanks.

[This message has been edited by Rahul Mahindrakar (edited January 29, 2001).]
Rahul Mahindrakar
Ranch Hand

Joined: Jul 28, 2000
Posts: 1861
Hi Satya,
You are right when you stated there is something wrong with my code. Indeed it does not do the work i expected to do.
Lets rewrite what i want to prove by my code
I want to increment a number by 1 1000 times. This is the JOB. I can do it using Threads or by using a method call. I can create Threads by using start() of the thread class. I can use a method call by invoking the run() method directly. In the second case no new thread is created.
I test out the time taken in the two cases. However because the II case is very fast and i want to clock some time i increase the number increment by another 1000 times .
What i missed out in my code is the following two lines.
followed by a loop containing

I have made the changes such that the end time is when all the threads have completed their work. This is why the join has been used. This is not there in the earlier version of the code.

Thread t =new Thread(temp);//I pulled out this statement
for (int i=0;i<100000 ;i++ ){;//I'm starting new thread instead of creating new object

In the code you have specified only a single thread to do work. This cannot be clocked adequately. Plus you have not provided for the fact that when this particular thread will complete its work. whether it has completed or not is not taken into account.

[This message has been edited by Rahul Mahindrakar (edited January 29, 2001).]
I agree. Here's the link:
subject: Threads - enough is enough