NIO dual-thread processing server prototype complete source code (continued)

Source: Internet
Author: User

A write thread is added, which is complete this time.

The following files have been modified, but others have not changed.

 

1. Server

Package server;

Import java. Io. ioexception;
Import java.net. inetsocketaddress;
Import java. NiO. channels. selectionkey;
Import java. NiO. channels. selector;
Import java. NiO. channels. serversocketchannel;

Public class server {

/**
* @ Param ARGs
*/
Public static void main (string [] ARGs ){
// Todo auto-generated method stub

Inetsocketaddress address = new inetsocketaddress ("127.0.0.1", 8080 );

Try {
Selector acceptselector = selector. open ();
Selector ioselector = selector. open ();

Serversocketchannel sschannel = serversocketchannel. open ();
Sschannel. configureblocking (false );
Sschannel. socket (). BIND (Address );
Sschannel. Register (acceptselector, selectionkey. op_accept );

Acceptthread = new acceptthread ();
Iothread = new iothread ();
Writethread = new writethread ();

Iothread. setselector (ioselector );
Iothread. setthread (writethread );
Acceptthread. setacceptselector (acceptselector );
Acceptthread. setioselector (ioselector );
Writethread. setselector (ioselector );


Iothread. Start ();
Acceptthread. Start ();
Writethread. Start ();

Iothread. Join ();
Acceptthread. Join ();
Writethread. Join ();

} Catch (ioexception e ){
// Todo auto-generated Catch Block
E. printstacktrace ();
} Catch (interruptedexception e ){
// Todo auto-generated Catch Block
E. printstacktrace ();
}


}

}

 

2. iothread

Package server;

Import java. Io. ioexception;
Import java. NiO. bytebuffer;
Import java. NiO. channels. closedchannelexception;
Import java. NiO. channels. selectionkey;
Import java. NiO. channels. selector;
Import java. NiO. channels. socketchannel;
Import java. util. iterator;
Import java. util. Set;

Public class iothread extends thread {
Private selector;
Private writethread;
 
Public void setselector (selector ){
This. selector = selector;
}
 
Public void run (){
Bytebuffer buffer = bytebuffer. Allocate (1024 );
While (true ){
Int keys = 0;
Try {
Keys = selector. Select ();
} Catch (ioexception e ){
// Todo auto-generated Catch Block
E. printstacktrace ();
}


If (Keys> 0 ){
Set <selectionkey> selectedkeys = selector. selectedkeys ();
Iterator <selectionkey> it = selectedkeys. iterator ();

While (it. hasnext ()){
Selectionkey key = it. Next ();
If (key. isreadable ()){
System. Out. println ("IO: Read ");
Socketchannel channel = (socketchannel) Key. Channel ();
Writethread. setchannel (Channel );
Try {
Buffer. Clear ();
Int I = channel. Read (buffer );
System. Out. println (I );
If (I <0) channel. Close ();
} Catch (ioexception e ){
// Todo auto-generated Catch Block
E. printstacktrace ();
}
// Key. interestops (selectionkey. op_write );
/*
Try {
Channel. Register (selector, selectionkey. op_write );
} Catch (closedchannelexception e ){
// Todo auto-generated Catch Block
E. printstacktrace ();
}
*/
} Else if (key. iswritable ()){
System. Out. println ("IO: write ");
Socketchannel channel = (socketchannel) Key. Channel ();
// Key. interestops (selectionkey. op_read );
Try {
Channel. Register (selector, selectionkey. op_read );
} Catch (closedchannelexception e ){
// Todo auto-generated Catch Block
E. printstacktrace ();
}
}
It. Remove ();
}
}
}
}

Public void setthread (writethread ){
// Todo auto-generated method stub
This. writethread = writethread;
}
}

 

3. writethread

Package server;

Import java. NiO. channels. closedchannelexception;
Import java. NiO. channels. selectionkey;
Import java. NiO. channels. selector;
Import java. NiO. channels. socketchannel;

Public class writethread extends thread {
Private socketchannel channel;
Private selector;
 
Public void setchannel (socketchannel channel ){
This. Channel = channel;
}

Public void setselector (selector ){
This. selector = selector;
}
 
Public void run (){

While (true ){
If (Channel! = NULL ){
Try {
Channel. Register (selector, selectionkey. op_write );
} Catch (closedchannelexception e ){
// Todo auto-generated Catch Block
E. printstacktrace ();
Break;
}

} Else {
Try {
This. Sleep (1000 );
} Catch (interruptedexception e ){
// Todo auto-generated Catch Block
E. printstacktrace ();
}
}
}
}
}

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.