Home arrow static arrow Java Programming [Archive] - Spinoff Discussion: Are Too Many New Threads Bad?
Warning: Creating default object from empty value in /www/htdocs/w008deb8/wiki/components/com_staticxt/staticxt.php on line 51
Java Programming [Archive] - Spinoff Discussion: Are Too Many New Threads Bad?
This topic has 15 replies on 2 pages.    1 | 2 | Next »

Posts:340
Registered: 7/8/04
Spinoff Discussion: Are Too Many New Threads Bad?  
Aug 7, 2004 7:50 PM



 
Inspired by Kaj's advice:

<quote>
It's usually better to keep the threads alive, if you just want to re-start them later. And in your case you have mentioned that it is for a game. And I guess that the game has a life cycle that is repeated. starting -> playing game. -> game over - > staring -> ...

That indicates that you should keep your threads alive.
</quote>

In trying to create a Runnable class whose thread could be easily stopped and restarted without dying I ran into some trouble. I'm not sure if the concept is flawed or if my implemenation is bad. In any case, I'd appreciate any comments on:

1. The test code below.
2. The idea of starting and stopping (but never killing) Threads in general. In particular, the object's state (ie, the values of its members which can be changed by the thread) seems to present a problem. When you create a new Thread on a new object, you get default values for all the members, which is what I want in my case. If you were manually starting and stopping the thread, you would have to create a special reset() method that your object's custom start() method could call. This seems messy. The run method in the test code below also seems messy. Are these design warts really compensated for by the overhead you save by never creating unnecessary threads?
public class Test implements Runnable{ 	Thread mThread;	boolean mStopped;	public Test() {		mThread = new Thread(this);		mThread.start();	}	public synchronized void start() {		mStopped = false;		notifyAll();	}	public synchronized void stop() {		mStopped = true;	} 	public synchronized void run() {		while (true) {			//give other threads a chance to start and stop this one			try { wait(10); }			catch (InterruptedException e) { System.out.println("Interrupted Exception!"); }			//if it does get stopped, pause here till its restarted			if (mStopped) {				System.out.println("Stopping...");				try { wait(); }				catch (InterruptedException e) { System.out.println("Interrupted Exception!"); }			}			System.out.println("Testing...");		}		} 	public static void main (String[] args) {		Test t = new Test();		try { Thread.currentThread().sleep(1000); }		catch (InterruptedException e) { System.out.println("Interrupted Exception!"); }		t.stop();		try { Thread.currentThread().sleep(1000); }		catch (InterruptedException e) { System.out.println("Interrupted Exception!"); }		t.start();		try { Thread.currentThread().sleep(1000); }		catch (InterruptedException e) { System.out.println("Interrupted Exception!"); }		t.stop();	}}
 

Posts:3,081
Registered: 2/15/99
Re: Spinoff Discussion: Are Too Many New Threads Bad?  
Aug 8, 2004 1:43 AM (reply 1 of 15)



 
Before starting to optimize: measure, measure, measure.

I get about 2000 threads starts & stops per second on a Windows laptop. Test program below.

So unless you are starting hundreds of threads per second, don't bother messing with thread pooling. You'd be optimizing something that already is fast enough.
public class t    extends Thread{    public void run()    {    }     public static void main(String args[])    {        int count = 1000;        Thread threads[] = new Thread[count];         for (int n = 0; n < 10; n++) {            long start = System.currentTimeMillis();            for (int m = 0; m < count; m++) {                Thread x = new t();                x.start();                threads[m] = x;            }            for (int m = 0; m < count; m++) {                try {                    threads[m].join();                } catch (InterruptedException e) {                    e.printStackTrace();                }            }            long end = System.currentTimeMillis();            System.out.println("time " + (end - start));        }    }}
 

Posts:18,384
Registered: 21.03.00
Re: Spinoff Discussion: Are Too Many New Threads Bad?  
Aug 8, 2004 2:08 AM (reply 2 of 15)



 
I get about 2000 threads starts & stops per second on
a Windows laptop. Test program below.

So unless you are starting hundreds of threads per
second, don't bother messing with thread pooling.
You'd be optimizing something that already is fast
enough.

Sjasja,
It's a fact that threads are expensive. They all need to have a stack of their own, so they consume a lot of memory, and they need to be scheduled etc. If you tell a game developer that you are thinking about having a thread for each bullet in a game he will laugh his head off. It's just bad design. How many threads do you think doom 3 has? thousands? I bet not.

HotQuietDay give a short but complete example of what you want to do, and I will show you how to do it.

/Kaj
 

Posts:3,081
Registered: 2/15/99
Re: Spinoff Discussion: Are Too Many New Threads Bad?  
Aug 8, 2004 3:12 AM (reply 3 of 15)



 
It's a fact that threads are expensive. They all need
to have a stack of their own, so they consume a lot of
memory, and they need to be scheduled etc. If you tell
a game developer that you are thinking about having a
thread for each bullet in a game he will laugh his
head off. It's just bad design.

I agree: a large number of threads can be problematic. On workstation/server -class systems a few dozen is ok, a few hundred starts getting problematic.

However, the OP wasn't discussing number of threads; the subject was whether threads should be kept alive (stopped & restarted).

Before trying to optimize thread starting & stopping: measure, measure, measure. Make back-of-the-envelope calculations: how many threads per second do you intend to start & stop. Then make a decision whether you need to optimize it.

I measure about half a millisecond here for start + run + stop + join (depends very much on OS, of course).

To wit:

It's usually better to keep the threads alive,
if you just want to re-start them later

Not necessarily. It takes, say, 0.0005 seconds to start a thread (measure your environment if you are targeting e.g. a cell phone!) If you stop a thread, then re-start it a minute later, you are probably being quite silly to write the "suspend" code to save 0.0005 seconds once per minute. Spend your code optimizing effort elsewhere.

If you are stopping a thread for 0.001 seconds, then you could save a measurable percentage if you don't really stop it.

How many threads do
you think doom 3 has? thousands? I bet not.

How much do you think 1+1 is, 3? It's not, you are sooooo wrong. See, two people can play this game: make a silly claim about what the other person thinks, then prove he is wrong. Let's not do that, shall we?
 

Posts:18,384
Registered: 21.03.00
Re: Spinoff Discussion: Are Too Many New Threads Bad?  
Aug 8, 2004 3:27 AM (reply 4 of 15)



 
Hi,

I haven't said that it is slow to start a thread. Just that a thread allocates lots of resources, and that is something that the gc later on has to deal with. I still do think that the OP has some thing wrong in his design (read the original thread, named some thing like stack overflow)

/Kaj
 

Posts:37,103
Registered: 3/30/99
Re: Spinoff Discussion: Are Too Many New Threads Bad?  
Aug 8, 2004 4:04 AM (reply 5 of 15)



 
Hi,

I haven't said that it is slow to start a thread. Just
that a thread allocates lots of resources, and that is
something that the gc later on has to deal with.

Just like with the speed issue, "lots of resources" is a relative term. The same rule applies: make some rough calculations and some measurements before rushing off to optimize something. Allocation and GC isn't necessarily the terrible performance killer it used to be.

http://www-106.ibm.com/developerworks/java/library/j-jtp01274.html

Nobody's saying that creating hundreds or thousands of threads will never be problematic, but your response to "first measure, then optimize" seems to be, "having lots of threads is always bad."
 

Posts:18,384
Registered: 21.03.00
Re: Spinoff Discussion: Are Too Many New Threads Bad?  
Aug 8, 2004 4:15 AM (reply 6 of 15)



 
Hi,

No, it's not always bad, but it's seldom needed. Many developers are using threads too much, and to things which you don't need them for.
And I'm not a fan of; first write your program, and the optimize. The fastest executing code is the one which isn't there at all. Optimization when you are done won't help if you have a bad design. As a consultant I often profile systems written by others, and that has the drawback that you will find hotspots within the running code, but you will not find out that the system might have a design flaw. So I have to say if you want to build a robust, fast an reliable system you have to think during ALL phases of development. Speed isn't a thing you try to archieve when you are done building the system. It's too late.

/Kaj
 

Posts:18,384
Registered: 21.03.00
Re: Spinoff Discussion: Are Too Many New Threads Bad?  
Aug 8, 2004 4:26 AM (reply 7 of 15)



 
http://www-106.ibm.com/developerworks/java/library/j-jt
01274.html

By the way, it's a good article, and I don't say that you should avoid object allocations, but rather that you should think twice before you create threads. Do I need it? Can I keep the old thread running instead? In my opinion you shouldn't think the same way when it comes to objects and threads. A new thread isn't just a new object.

/Kaj
 

Posts:340
Registered: 7/8/04
Re: Spinoff Discussion: Are Too Many New Threads Bad?  
Aug 8, 2004 10:48 AM (reply 8 of 15)



 
Thanks everyone for the discussion.

In my particular case, I will never have more than about 20-30 threads going at one time. My question was about wheather I should start and stop those 20-30 threads, or let them die and recreate them as needed. If the thread creation is under a millisecond, it seems to me the simplicity of design I'd gain by recreating threads is worth the extra half millisecond of effort. That is, I won't have to put those ugly if ... wait() statements in my run() methods, and I won't have to create reset() methods for the Runnable objects to get them back to a default state.

The case might be different if it were a matter of creating hundreds of threads.

Kaj, I'm guessing even with your conservative view of thread creation you might agree here??

Thanks again for the enlightening thoughts,
John
 

Posts:18,384
Registered: 21.03.00
Re: Spinoff Discussion: Are Too Many New Threads Bad?  
Aug 8, 2004 11:02 AM (reply 9 of 15)



 
Hi,

Don't be to sure about that :)
Why do you need 20-30 threads to run a game? It's very easy to create threads in java, but it's also very easy to create race conditions, deadlocks, etc. Even the people at sun agree to that, and that is why the concurrency package is added in Java 5.

When I have created small games (like pacman etc) I have only needed a couple of threads. One for updating the ui, one for playing sounds, and then you might need another one for reading the keyboard. That's about it.

/Kaj
 

Posts:37,103
Registered: 3/30/99
Re: Spinoff Discussion: Are Too Many New Threads Bad?  
Aug 8, 2004 11:08 AM (reply 10 of 15)



 
And I'm not a fan of; first write your program, and
the optimize. The fastest executing code is the one
which isn't there at all. Optimization when you are
done won't help if you have a bad design.

Optimizing before you know how much it will help has a very hight probability of not helping noticably at all.

I suspect we simply have a different view of what "optimizaion" means. To me, optimization means making a given section of code as fast as possible, even at the cost of clarity or clean design. It's quite true that optimization won't help if you have poor data structures and algorithms. Selecting quicksort over bubblesort is something I'd do as I write the code, but it's not what I'd call optimization.
 

Posts:37,103
Registered: 3/30/99
Re: Spinoff Discussion: Are Too Many New Threads Bad?  
Aug 8, 2004 11:17 AM (reply 11 of 15)



 
In my particular case, I will never have more than
about 20-30 threads going at one time. My question
was about wheather I should start and stop those 20-30
threads, or let them die and recreate them as needed.
If the thread creation is under a millisecond, it
seems to me the simplicity of design I'd gain by
recreating threads is worth the extra half
millisecond of effort.

That's how I'd advise doing it. If your design calls for 20-30 live threads (and let's assume for now that that's a sound design), and if it's cleaner, easier, and "more natural" for run() to do it's thing and then complete than to implement some kind of queue or thread pool, then go with that.

However, I'd recommend two things: 1) make your design modular enough so that if you have to swap out that model for a thread pool type of model that you can do so without touching other code and 2) test the performance of that piece as early as possible. Quick & dirty proof of concepts and prototypes early on can sometimes save lots of rework later.

That is, I won't have to put
those ugly if ... wait() statements in my run()
methods, and I won't have to create reset() methods
for the Runnable objects to get them back to a
default state.

The case might be different if it were a matter of
creating hundreds of threads.

Kaj, I'm guessing even with your conservative view of
thread creation you might agree here??

Thanks again for the enlightening thoughts,
John
 

Posts:340
Registered: 7/8/04
Re: Spinoff Discussion: Are Too Many New Threads Bad?  
Aug 8, 2004 12:25 PM (reply 12 of 15)



 
However, I'd recommend two things: 1) make your design
modular enough so that if you have to swap out that
model for a thread pool type of model that you can do
so without touching other code and 2) test the
performance of that piece as early as possible. Quick
& dirty proof of concepts and prototypes early on can
sometimes save lots of rework later.

I think both of those ideas are sound advice.

Cheers,
John
 

Posts:340
Registered: 7/8/04
Re: Spinoff Discussion: Are Too Many New Threads Bad?  
Aug 8, 2004 12:29 PM (reply 13 of 15)



 
Why do you need 20-30 threads to run a game?
When I have created small games (like pacman etc) I
have only needed a couple of threads.

The question was never: Do I need this many threads? The question is: Given that I need this many threads, should I keep them always alive or let them die and create new ones as needed?

By the way, do you have any comments on the implementation for keeping threads alive that I posted in the test code? If I do ever go the route of keeping threads alive, I'd like to do it right.

thanks,
john
 

Posts:18,384
Registered: 21.03.00
Re: Spinoff Discussion: Are Too Many New Threads Bad?  
Aug 8, 2004 12:36 PM (reply 14 of 15)



 
Hi,

No, I wouldn't write it like that. That's why I asked what you wanted to do, so I could show you some code. I don't think I ever would stop the threads (will, I would when the game terminates).
What you have tried to implement above looks more like a thread that serves runnables in a thread pool. And if that is what you want to do, then I would recommend to use the concurrency package by Doug Lea:
http://gee.cs.oswego.edu/dl/classes/EDU/oswego/cs/dl/util/concurrent/intro.html

It's the package that is also included in Java 5.

/Kaj
 
This topic has 15 replies on 2 pages.    1 | 2 | Next »