Java-Gaming.org    
Featured games (79)
games approved by the League of Dukes
Games in Showcase (477)
Games in Android Showcase (106)
games submitted by our members
Games in WIP (533)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Asynchronous TCP or poll?  (Read 1847 times)
0 Members and 1 Guest are viewing this topic.
Offline kaffiene
« Posted 2003-12-09 02:06:31 »

Hi,

I have a bunch of threads which can read and write over a TCP/IP stream.  Does anyone know whether it is better to get each of them to do asynchronous IO via NIO or poll them by setting a timeout on the socket?

Is there anyway to do a genuine poll of a socket? (the timeout option has to be >0 ms)
Offline blahblahblahh

JGO Coder


Medals: 1


http://t-machine.org


« Reply #1 - Posted 2003-12-09 07:27:39 »

Quote
Hi,
I have a bunch of threads which can read and write over a TCP/IP stream.  Does anyone know whether it is better to get each of them to do asynchronous IO via NIO or poll them by setting a timeout on the socket?


Since you ask (although I'm not sure it's quite what you wanted), it's always better to use NIO. In many cases it's quicker (to write the code) to use IO, but there's little other reason to use anything but NIO.

You should NEVER EVER EVER poll ANYTHING ANYWHERE if you want to achieve high performance anything. It's one of the gross generalizations of performance tuning which is nearly always true - along with things like "Writing large programs in Visual Basic will always result in memory hungry and slow beasts" Wink Well, something like that Smiley.

By definition, polling wastes time - if you are very lucky or have a very specific situation it will be no worse than non-polling, but in general there will be many more polls than you needed to do (note: if you reduce the poll rate, by definition you simultaneously reduce the responsiveness, which is almost always undesirable)

Quote

Is there anyway to do a genuine poll of a socket? (the timeout option has to be >0 ms)


AFAIAA there is no way to do this using standard libs; however, this is based on experimentation long ago in the past, so I may be completely wrong here Smiley.

Why do you want to do a "genuine" poll, though? One of the many good reasons for high-level networking API's a la java standard libs is that 99.999% of the time you have no use for low-level API's (network sniffers are the main counter example). The best reason I can come up for wanting access to poll is that you want to benchmark low-level aspects of a system?

malloc will be first against the wall when the revolution comes...
Offline kaffiene
« Reply #2 - Posted 2003-12-09 17:56:56 »

Quote


Why do you want to do a "genuine" poll, though? One of the many good reasons for high-level networking API's a la java standard libs is that 99.999% of the time you have no use for low-level API's (network sniffers are the main counter example). The best reason I can come up for wanting access to poll is that you want to benchmark low-level aspects of a system?


Well, I have written high performance servers before in C/C++ in the telecommunications arena.  These have either being blocking, genuine polling or multiplexed via select().  All of these approaches have been extremely fast and each of them was best suited to the task it was employed for.

In my particular instance, each thread will be managing one TCP/IP connection, so mutliplexing with select() seems a bit heavy (since there are no other connections to select on)  so that's why I was wondering about polling (which would be simpler to implement).  

If NIO is the best fit, that's fine.  
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline blahblahblahh

JGO Coder


Medals: 1


http://t-machine.org


« Reply #3 - Posted 2003-12-10 21:21:55 »

Quote

Well, I have written high performance servers before in C/C++ in the telecommunications arena.  These have either being blocking, genuine polling or multiplexed via select().  All of these approaches have been extremely fast and each of them was best suited to the task it was employed for.


If you read my reply carefully you'll see that I noted exceptions at several points; for brevity I didn't expand on them. I wasn't trying to imply you were being stupid (sorry if it looked like that), just that it was very unlikely you really wanted to use poll - you provided pretty much 0 information about what you are doing or why, forcing us to guess; most people posting a vague non-specific question on performance are doing so because they are novices and too naive to realise that their question will be unanswerable without much more info; hence I pitched my answer appropriately.

Many many poll-based systems are deployed in completely inappropriate situations whereas very few are appropriate - hence it is "nearly always true" that you should avoid it (like re-writing a standard library; if you think you need to, it's worth double and triple checking your reasons).

...Unless you don't care about performance, but it seemed from your post that you do.

Quote

In my particular instance, each thread will be managing one TCP/IP connection, so mutliplexing with select() seems a bit heavy (since there are no other connections to select on)  so that's why I was wondering about polling (which would be simpler to implement).  


But...why must you have a thread per connection? Again, making a guess at what you're really asking here, I'd say that your question is irrelevant compared to the potential savings of getting rid of your threads.

How "heavy" do you fear multiplexed select to be?

W.r.t. performance, NIO select is not just for multiplexing (that's often just a bonus).

malloc will be first against the wall when the revolution comes...
Offline blahblahblahh

JGO Coder


Medals: 1


http://t-machine.org


« Reply #4 - Posted 2003-12-10 21:27:16 »

Quote
Hi,

I have a bunch of threads which can read and write over a TCP/IP stream.  Does anyone know whether it is better to get each of them to do asynchronous IO via NIO or poll them by setting a timeout on the socket?

Is there anyway to do a genuine poll of a socket? (the timeout option has to be >0 ms)



  • how many is a bunch?
  • what are your traffic patterns?
  • why is polling desirable? (you've said it's less hassle to code so far...is that the only reason?)
  • what are the threads doing?
  • What are the options you are evaluating? "do asynchronous IO ... or poll them" hides all the detail necessary to give you an answer;
  • Most importantly of all...what is "better"? Less memory usage, smaller thread-context, more thread-level-parallelism, more transparent code, lower latency, higher processing throughput ... the list goes on. Hence my tongue-in-cheek first para in first reply...


malloc will be first against the wall when the revolution comes...
Offline kaffiene
« Reply #5 - Posted 2003-12-10 22:19:19 »

Blahblahblah: Don't worry - I didn't take offense at your reply.

I'm coding for a server which will serve several clients (btw: a "bunch" equals 2-16 in this case :-) ). The server side representation was going to be a single thread per client which I had thought could just poll for input when nudged by the server - hence wondering about whether reading could be done with a poll rather than a timeout.  

I've been thinking about what I need the server to do and I've decided that threads are probably unneccesary and that I probably want to go with UDP anyway (don't need in-order delivery and most taffic won't need acking), so I've kinda rendered this thread redundant.  Smiley

Sorry if I was being unclear in my question - probably a reflection of me not having thought enough about what I wanted to achieve.
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

pw (24 views)
2014-07-24 01:59:36

Riven (22 views)
2014-07-23 21:16:32

Riven (18 views)
2014-07-23 21:07:15

Riven (21 views)
2014-07-23 20:56:16

ctomni231 (50 views)
2014-07-18 06:55:21

Zero Volt (45 views)
2014-07-17 23:47:54

danieldean (36 views)
2014-07-17 23:41:23

MustardPeter (39 views)
2014-07-16 23:30:00

Cero (54 views)
2014-07-16 00:42:17

Riven (54 views)
2014-07-14 18:02:53
HotSpot Options
by dleskov
2014-07-08 03:59:08

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:58:24

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:47:22

How do I start Java Game Development?
by ra4king
2014-05-17 11:13:37

HotSpot Options
by Roquen
2014-05-15 09:59:54

HotSpot Options
by Roquen
2014-05-06 15:03:10

Escape Analysis
by Roquen
2014-04-29 22:16:43

Experimental Toys
by Roquen
2014-04-28 13:24:22
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!