A small example of using a thread pool to process socket requests

Source: Internet
Author: User

Task Description: A program is designed to listen to port 8790 of the Local Machine. When a socket is connected, a thread pool is used to create a processing thread. After processing, a processing thread is returned and the main program is not involved in the service.

 

Paste the Code directly:

 

Master Server

 

Package COM. SDC. callmaxent. socket; <br/> Import Java. io. ioexception; <br/> Import java.net. serversocket; <br/> Import java.net. socket; <br/> Import Java. util. concurrent. arrayblockingqueue; <br/> Import Java. util. concurrent. threadpoolexecutor; <br/> Import Java. util. concurrent. timeunit; <br/> Import COM. SDC. callmaxent. util. systemconfig; <br/> public class callmaxentthreadpool {<br/> Private Static int producetasksleeptime = 20; <br/> Private Static int producetaskmasknumber = 10; </P> <p> Private Static Boolean flag = false; </P> <p> Public static void main (string [] ARGs) {<br/> startserv (); <br/> // construct the thread pool <br/> system. out. println ("server listening ...... "); </P> <p> int Port = systemconfig. getinstance (). getport (); <br/> serversocket = NULL; <br/> try {<br/> serversocket = new serversocket (port ); <br/>} catch (ioexception E1) {<br/> e1.printstacktrace (); <br/>}< br/> socket clientsocket = NULL; <br/> threadpoolexecutor threadpool = new threadpoolexecutor (<br/> 2, // corepoolsize <br/> 4, // maximumpoolsize <br/> 3, // KeepAliveTime <br/> timeunit. seconds, // Unit <br/> New arrayblockingqueue <runnable> (3), // workqueue <br/> New threadpoolexecutor. discardoldestpolicy () // <br/>); </P> <p> int I = 0; <br/> while (FLAG) {<br/> // obtain the client request <br/> try {<br/> clientsocket = serversocket. accept (); <br/>}catch (ioexception e) {<br/> E. printstacktrace (); <br/>}< br/> string task = "maxent_task @" + I; <br/> system. out. println ("put" + task); <br/> threadpool.exe cute (New callmaxentthreadpooltask (clientsocket); <br/> I ++; <br/>}</P> <p> Public static void startserv () {<br/> flag = true; <br/>}</P> <p> Public static void stopserv () {<br/> flag = false; <br/>}< br/>}

 

Process thread Program

 

Package COM. SDC. callmaxent. socket; <br/> Import Java. io. bufferedreader; <br/> Import Java. io. ioexception; <br/> Import Java. io. inputstream; <br/> Import Java. io. inputstreamreader; <br/> Import Java. io. outputstream; <br/> Import Java. io. printwriter; <br/> Import Java. io. serializable; <br/> Import java.net. socket; <br/> Import COM. SDC. callmaxent. util. fileutil; <br/> Import COM. SDC. callmaxent. util. socketutil; <B R/> Import COM. SDC. callmaxent. util. systemconfig; <br/> public class callmaxentthreadpooltask implements runnable, serializable {<br/> Private Static final long serialversionuid = 0; <br/> Private Static int consumetasksleeptime = 20000; </P> <p> private Socket socket; <br/> private inputstream clientinput; <br/> private outputstream clientoutput; </P> <p> callmaxentthreadpooltask (Socket socket) {<br/> thi S. socket = socket; </P> <p> try {<br/> This. clientinput = socket. getinputstream (); <br/> This. clientoutput = socket. getoutputstream (); <br/>} catch (ioexception e) {<br/> E. printstacktrace (); <br/>}</P> <p> Public void run () {<br/> bufferedreader breader = fileutil. getbufferreader (New inputstreamreader (clientinput); <br/> try {<br/> // receives information from the client <br/> string strline = breader. readline (); <br/> // parameter segmentation <Br/> string [] paramlist = strline. split (","); <br/> string maxentpath = systemconfig. getinstance (). getmaxentpath (); </P> <p> string cmd = "CMD/C Java-mx1024m-jar" + maxentpath + "/maxent. jar "; </P> <p> for (INT I = 1; I <paramlist. length; I ++) {<br/> cmd + = "" + paramlist [I]; <br/>}< br/> system. out. println (thread. currentthread (). getname (); <br/> system. out. println ("maxent" + paramlist [0] + "is running, pleas E wait! "); </P> <p> cmd + =" novisible Autorun "; </P> <p> PROCESS p = runtime.getruntime(cmd.exe C (CMD); <br/> P. waitfor (); <br/> printwriter = new printwriter (clientoutput, true); <br/> printwriter. println ("finish! "); <Br/> system. Out. println (" task "+ paramlist [0] +" finish! "); <Br/>}catch (ioexception e) {<br/> E. printstacktrace (); <br/>} catch (interruptedexception e) {<br/> E. printstacktrace (); <br/>}finally {<br/> fileutil. close (clientinput); <br/> fileutil. close (clientoutput); <br/> socketutil. close (socket); <br/>}< br/>

 

Test Client

 

Package COM. SDC. callmaxent. socket; <br/> Import Java. io. bufferedreader; <br/> Import Java. io. ioexception; <br/> Import Java. io. inputstream; <br/> Import Java. io. inputstreamreader; <br/> Import Java. io. outputstream; <br/> Import Java. io. printwriter; <br/> Import java.net. socket; <br/> Import java.net. unknownhostexception; <br/> Import COM. SDC. callmaxent. util. fileutil; <br/> Import COM. SDC. callmaxent. util. socketutil; <br/> Import COM. SDC. callmaxent. util. systemconfig; <br/> public class callmaxentclient {<br/> Public static void main (string [] ARGs) {<br/> try {<br/> system. out. println ("start sending ...... "); </P> <p> string IPaddress = systemconfig. getinstance (). getip (); <br/> int Port = systemconfig. getinstance (). getport (); </P> <p> Socket socket = socketutil. getsocket (IPaddress, Port); <br/> bufferedreader bfreader = fileutil. getbufferreader (New inputstreamreader (system. in); <br/> system. out. println ("waiting ..... "); <br/> string strline =" 1, environmentallayers = F:/test/maxent/testdata/layers, samplesfile = F:/test/maxent/testdata/bradypus.csv, outputdirectory = F:/test/maxent/testdata/outputs, togglelayertype = ecoreg, redoifexists "; <br/> // string strline =" environmentallayers = F: /test/maxent/testdata/layers, samplesfile = F:/test/maxent/testdata/bradypus.csv, outputdirectory = F:/test/maxent/testdata/outputs2, togglelayertype = ecoreg, redoifexists "; </P> <p> outputstream = socket. getoutputstream (); <br/> inputstream = socket. getinputstream (); </P> <p> printwriter PW = new printwriter (outputstream, true); </P> <p> PW. println (strline); <br/> bufferedreader BR = new bufferedreader (New inputstreamreader (inputstream); <br/> system. out. println (BR. readline (); <br/>} catch (unknownhostexception e) {<br/> E. printstacktrace (); <br/>}catch (ioexception e) {<br/> E. printstacktrace (); <br/>}< br/>

 

Thread pool execution priority

 

Corepoolsize> workqueue> maximumpoolsize-> Handler

 

 

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.