Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > java.nio as opposed to java.net - basic difference in program logic?

Reply
Thread Tools

java.nio as opposed to java.net - basic difference in program logic?

 
 
Chris Berg
Guest
Posts: n/a
 
      11-23-2003
My client/server application is using plain sockets, in the
traditional way:

ServerSocket ss=new ServerSocket(port);
while (true){
new Handler(ss.accept());
}

......

class Handler{

Socket s;

public Handler(Socket s){
this.s=s;
new Thread(this).start();
}

public void run(){
while(true){
read a command from s's inputstream;
interpret it;
write a reply to s's outputstream;
if (user logs off) break;
}
}

}

Of course, there is more to it - setting the socket timeout, etc., but
I'm sure you get the picture.

I worry about how my server will react to many simultaneous clients,
each with not too much activity, but there may be a lot of concurrent
threads (thousands), consuming many system ressources.

Now, I'f noticed the new java.nio classes that seem to handle multiple
clients in a more efficient way. Only, I'm concerned that I will have
to completely re-write the program logic to make it fit into the
scheme. As everything runs on the same thread, I must program it in a
more event-handler-oriented manner, where I need to save a state
vector for every client, which would roughly correspond to the current
program location of each of the old threads. Or is there maybe a
smarter way to do that?

Also, some of the command-handling code can be blocking (ie.
Thread.sleep() or disk file I/O). This means that I'll have to leave
the handler and go back to the Selector and service incoming commands
before having finished current commands. What a mess! It must end up
with some ugly code!??

Chris

 
Reply With Quote
 
 
 
 
Sudsy
Guest
Posts: n/a
 
      11-23-2003
Chris Berg wrote:
> My client/server application is using plain sockets, in the
> traditional way:

<snip>
> Also, some of the command-handling code can be blocking (ie.
> Thread.sleep() or disk file I/O). This means that I'll have to leave
> the handler and go back to the Selector and service incoming commands
> before having finished current commands. What a mess! It must end up
> with some ugly code!??
>
> Chris


The java.nio classes permit me to write code similar to a UNIX
daemon which uses select(). The Jave implementation is actually
a bit nicer since I don't have to do a fork()/exec() to start a
subprocess; I can just do new Thread( Runnable ).start(). Less
overhead.
It doesn't have to be messy if you clearly understand your goals.
Supposedly you'd want to handle the "pedestrian" requests in the
main thread and only create a new thread for those operations
which could take some time to complete.
Think of an Apache/Tomcat installation. When configured in the
"standard" way, Apache takes care of serving requests for static
content and only the active content requests get forwarded to
Tomcat.
If certain requests could block on disk or network I/O then they
deserve their own thread. If this traffic represents only 10% of
the total then you'll be ahead of the game. Just map out your
architecture carefully.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
What is the difference between Visual Basic.NET and Visual Basic 6? Jimmy Dean Computer Support 3 07-25-2005 07:05 AM
LAMP & J2EE as opposed to LAMP vs J2EE Ross M. Greenberg Java 6 12-24-2004 09:59 PM
Help please - Why does ByteBuffer return '?' as opposed to what was put? Ed Java 5 10-30-2004 12:05 AM
SSL Performance HIT in REVERSE? the clients are PEGGED as opposed to the server? anon ASP .Net 5 05-19-2004 01:25 PM
XSL rules applying to XSD (XML schema) defined type names (as opposed to node names) Lewis G. Pringle, Jr. XML 0 09-30-2003 10:34 PM



Advertisments