From: Ben Schumacher Date: 2002-10-04T04:17:41+09:00 Subject: Re: Please help, question regarding threads Bulat Ziganshin wrote: > Hello Yohanes, > > Thursday, October 03, 2002, 12:09:01 PM, you wrote: > >>>I am working on a server-oriented application which is threaded, each >>>connection to the server creates a new thread >> > > YS> So, in the end, if you see that your number of threads are growing in > YS> step with the number of connection, perhaps it is time for you to > YS> re-evaluate the design. > > if he writes server, not client, it is better to use one thread per > connection. also, ruby threads are very light and IMHO you can't save > time by simulating thread switching in your own code Would you care to explain why? I can give you a great example of at least one protocol where setting a thread per connection is a terrible idea, XMPP (Jabber). Since the protocol expects the user to setup a long-running socket per user, even though the traffic across this socket is relatively low, a thread per connection would kill your scalability. If the usage of these sockets is short-lived, and there is a limited number of connections established, I can understand a thread per connection, as it make design significantly easier. However, this imposes scalability restrictions on your software at design time, and should you decide later you need more scalability in your software, you'll have to throw more money, either in the form of bigger hardware, or time refactoring. Admittedly, I don't know all the specifics around the design of ruby's thread implementation, but I would venture a guess that a thread per socket in any software where you need a highly scalable network server is a bad idea. Keep in mind, however, that the thread implementation in ruby (the C interpreter) is likely to be radically different from an implementation in JRuby (the Java interpreter). bs.