• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Threads - enough is enough

 
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 1871
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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,
-SaTyA-

Hi,
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
Posts: 1871
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
t.start();
followed by a loop containing
t.join();

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++ ){
t.run();//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).]
 
All of the world's problems can be solved in a garden - Geoff Lawton. Tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic