This article is from a small piece of software I wrote: Multi-threaded batch probing the type of server for each IP segment of the specified IP (apache,tomcat,iis ... ) and write the probe results to a file. So the question is: how to ensure that multiple threads are used in the loop increment of IP and that each IP is not reused for submission (PS: Of course, to avoid duplication, you can wait and wake with a thread, but I personally think this approach is not necessarily more than a single line Chengqiang how much)? At the same time, how can you avoid file garbled due to preemptive writes of individual threads when writing results to a file?
After some thinking, I think it's possible to think: for a continuous number segment of a loop, we can distribute the number evenly to each thread when the number of threads is given, so that each thread only needs to step through its task. because there is no coupling to the data of other threads, it is guaranteed that each number is processed only once and that there is no problem when the file is written.
> Next, I wrote a test code in this line of thought, the complete code is as follows:
Package thread;import java.io.bufferedwriter;import java.io.file;import java.io.filewriter ; Import java.io.ioexception;public class test1 {public static void main ( String[] args) {long millis1 = system.currenttimemillis (); int threadNum = 5; // number of threads used for testing string filename = "c:\\users\\administrator\\desktop\\ test 1.txt"; // file name for (int i = 0; i < threadnum; i++) {MyThread mythread = new mythread (I, threadnum, filename); Thread tempthread = new thread (MyThread); Tempthread.setname ("Thread" + i); Tempthread.start ();} Long millis2 = system.currenttimemillis (); System.out.println (MILLIS2&NBSP;-&NBSP;MILLIS1); //about 1-2ms}}/** * custom thread * */ class mythread implements runnable {private int i; // The first few threads private int threadnum; // a total of several threads created private string filename; // File name Public mythread (int i, int threadnum, string filename) {this.i = i;this.threadnum = threadnum;this.filename = filename;} Public void run () {new myprint (). print (I, threadnum, filename);}} /** * Specific business operations * */class myprint {private bufferedwriter writer;public void print (Int x, int threadnum, string filename) {try {writer = new bufferedwriter (New filewriter (New file (fileName), true)); // Append for If the file already exists (int i = x; i <= 10000; i = i + threadnum) {string temp = thread.currentthread (). GetName () + ": " + i+ "----------------------------------I am a gorgeous little tail";//sySTEM.OUT.PRINTLN (temp); Writer.write (temp); Writer.newline (); Writer.flush ();} Writer.close ();} catch (ioexception e) {e.printstacktrace ();}}
The test found that although only 5 threads were used, the execution efficiency was very high, and the 1~2ms could be completely processed.
Report:
for the same write information, the single-threaded code is this:
package thread;import java.io.bufferedwriter;import java.io.file;import java.io.filewriter;import java.io.ioexception;public class test { Public static void main (String[] args) {long millis1 = System.currenttimemillis (); string filename = "c:\\users\\administrator\\desktop\\ test 0.txt"; // file name try { Bufferedwriter writer = new bufferedwriter (New filewriter (New File (FileName), true)); for (int i=0;i<=100000;i++) {string temp = "single Thread: " + i+ "----------------------------------I am a gorgeous little tail"; writer.write (temp); Writer.newline (); Writer.flush ();} Writer.close ();} catch (ioexception e) {e.printstacktrace ();} long millis2 = system.currenttimemillis (); System.out.println (MILLIS2&NBSP;-&NBSP;MILLIS1); //approx. 162-168ms}}
After testing, it takes about 160 milliseconds to complete the execution, which is far less efficient than the multithreading above (∩_∩) o~
(PS: The introduction of the batch detection server type of software I will write in the following article, and will release a simple GUI interface and source code, please look forward to!) )
This article is from "Zifangsky's personal blog" blog, make sure to keep this source http://983836259.blog.51cto.com/7311475/1722422
Multi-threaded loop batch processing and multi-threaded operation file writing related ideas