1

I got really interested into making an application that could accept & handle I/O out of multiple connected clients.

I've been told that I need to use two threads for this, for the performance.

Thread-1

Accepting connections

Thread-2

Handling clients I/O and response

I've took a look at what is a thread, I did use it, but I am not really sure how can I split my program (Don't have one, but just saying) into two threads.

How can I do that?

One thread of these should have it's own class? I have no idea of a good design I can apply for this so I can understand it better.

How can I do this? can you show me a little example?

Jony Kale
  • 979
  • 3
  • 15
  • 35

1 Answers1

0

Here's an example with a complete tutorial that I hope will be helpful. Note how you have one thread (the main one, right from the start) and everytime a connection is received, a new thread of the type KKMultiServerThread is spawn.

Once you are familiar with this, consider using a Thread Pool rather than creation a new thread only when the connection starts.

Enjoy Java!

Miquel
  • 15,405
  • 8
  • 54
  • 87
  • So you want to create a new thread for each connection? What if i have 200 connections +? That doesn't so very good to me – Jony Kale Nov 29 '13 at 14:21
  • Well, you do want to serve 200 clients, so you will need 200 threads. You can optimize response times a little with the thread pool, but otherwise, you don't have another choice. Perhaps someone else comes up with a better idea though. – Miquel Nov 29 '13 at 14:23
  • In the C# chat, they told me to make a thread that handles connection and one thread that handles all user's. Did he say that because it's different in c#? – Jony Kale Nov 29 '13 at 14:24
  • 2
    @JonyKale This SO http://stackoverflow.com/questions/763579/how-many-threads-can-a-java-vm-support shows that modern VMs support thousands of threads. On such scale (thousands of clients), main bottleneck will be your processing logic, so you would need cluster of servers anyway. If you don't want much threads, you can use IO multiplexing (having less than one thread per client). In Java, this is available in `java.nio` package. – Victor Sorokin Nov 29 '13 at 14:27
  • @VictorSorokin And is IO multiplexing better for many clients? (performance) – Jony Kale Nov 29 '13 at 14:28
  • That again depends on your scenario, [see here](http://stackoverflow.com/questions/7611152/nio-performance-improvement-compared-to-traditional-io-in-java) – Miquel Nov 29 '13 at 14:30
  • 2
    Current public understanding is that traditional model `thread-per-client` has the same (or better) throughput, while being simpler to implement: http://mailinator.blogspot.ru/2008/02/kill-myth-please-nio-is-not-faster-than.html – Victor Sorokin Nov 29 '13 at 14:30
  • Since IO multiplexing requires less memory, that memory can be used for, say, database cache and so it may improve overall performance. But programming with threads is simpler, so you better start with threads. – Alexei Kaigorodov Nov 29 '13 at 16:02