Analysis of application of Java thread pool

Source: Internet
Author: User

Storage Data Tool class, dedicated to the main thread read up data

ImportJava.util.Queue;ImportJava.util.concurrent.ArrayBlockingQueue;/*** Save an array of strings to read on each main thread@authorWinkey **/ Public classBatchdataarrholder {Private Static Final intQueue_size = 1000000; Private StaticQueue<string[]> Data_queue =NewArrayblockingqueue<string[]>(queue_size);  Public Static voidpush (string[] data) {data_queue.add (data); }     Public Staticstring[] Take () {Try {            return((arrayblockingqueue<string[]>) data_queue). Take (); } Catch(interruptedexception e) {e.printstacktrace (); }        return NULL; }         Public Static BooleanIsEmpty () {returnData_queue.isempty (); }}

Main Program class:

ImportJava.io.BufferedReader;ImportJava.io.BufferedWriter;ImportJava.io.FileReader;ImportJava.io.FileWriter;Importjava.io.IOException;ImportJava.util.Map;ImportJava.util.concurrent.ExecutorService;Importjava.util.concurrent.Executors;/*** Multi-threaded character stream *@authorWinkey **/ Public classMyThread2extendsthread{ Public StaticSortarray Sortarray =NewSortarray (10000) ;  Public voidrun () {//This method is written so right???          while(true) {string[] Batchdataarr=Batchdataarrholder.take (); if(NULL= = Batchdataarr)Continue; if(NULL!=Batchdataarr) {                 for(inti=0;i<batchdataarr.length;i++) {String str=Batchdataarr[i]; Integer Numasint=integer.valueof (str); System.out.println (Thread.CurrentThread (). GetName ()+"__"+Numasint);
           The //setbit method is defined in the sort class, and I add the keyword synchronized to the Setbit method. sortarray.setbit (numasint, 1); } } } } Public Static voidMain (string[] args)throwsException {BufferedReader in=NewBufferedReader (NewFileReader ("E:\\b.txt")); BufferedWriter out=NewBufferedWriter (NewFileWriter ("E:\\cc.txt")); Batchdataarrholder Holder=NewBatchdataarrholder (); intReadlength = 0; Char[] chars =New Char[5]; String Tail=NULL; String Batchdata; Try{ //Create a thread pool that can reuse a fixed number of threadsExecutorservice pool = Executors.newfixedthreadpool (2);//two sub-threadsMyThread2 MT1 =NewMyThread2 (); MyThread2 mt2=NewMyThread2 ();
       ////The thread reads the data before the child thread starts, parses the data, so is it written right? Pool.execute (MT1); Pool.execute (MT2); while(true) {Readlength= In.read (chars, 0, chars.length); //not read the data indicates that the last read data has been read, no longer processed if(Readlength = = 0) Break; BooleanIsend =false; //The data read is less than the array length, indicating that this time has been read, after processing this time no longer continue to read if(Readlength <chars.length) { //System.out.println (string.valueof (chars) substring (0, readlength));Batchdata = string.valueof (chars). substring (0, readlength); Isend=true; }Else{ //System.out.println (string.valueof (chars));Batchdata =string.valueof (chars); } //the remaining data after the previous if(Tail! =NULL) {Batchdata= "" +tail+Batchdata; } //intercept the rest of the data and save it for the next time.Tail = batchdata.substring (Batchdata.lastindexof (",") +1, Batchdata.length ()); if(Tail.length () ==0) {Tail=NULL; } batchdata= Batchdata.substring (0,batchdata.lastindexof (",")); String[] Batchdataarr=NewString[batchdata.split (","). length];//multithreaded processing this thing!!! Batchdataarr = Batchdata.split (","); Holder.push (Batchdataarr);//The main thread saves the data that is read each time to if(isend==true){ Break; } } //Shut down threads, so close child threads, right??? while(true){ if(Batchdataarrholder.isempty ()) {Pool.shutdown (); Break; } } //Write DataInteger Sortunit =Sortarray.getmaxnum (); System.out.println (Sortunit); for(inti = 0;i<=sortunit;i++){ if(Sortarray.getbit (i) ==1) {StringBuffer buf=NewStringBuffer ("+i+" "); Buf.append (","); Integer Num=SortArray.repeatingData.get (i); if(num!=NULL&& num>=2){ for(intj=2;j<=num;j++) {buf.append ("" +i+ "). Append (", "); }} out.write (Buf.tostring ()); }} out.flush (); }finally{ if(in!=NULL){ Try{in.close (); }Catch(IOException e) {e.printstacktrace (); } } if(out!=NULL){ Try{out.close (); }Catch(IOException e) {e.printstacktrace (); } } } }}

Analysis of application of Java thread pool

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.