Java Multithreading write the same file implementation __java

Source: Internet
Author: User
Tags sleep thread class

Recently, the project needs to crawl a lot of data from the site, using multithreading technology, each thread crawl data need to be saved to a file, avoid consuming a lot of memory.

Idea: Multiple access threads will save the data that needs to be written to a file into a queue, and then write the data from the queue and write it to the file by a dedicated writing thread.


Writerqueue.java store data queues to output

Package com.yulore.write;
Import java.util.LinkedList;
Import java.util.concurrent.locks.Condition;
Import Java.util.concurrent.locks.Lock;

Import Java.util.concurrent.locks.ReentrantLock;
	public class Writerqueue {private static final int max_queue_size = 5000;
	Private linkedlist<string> queue = new linkedlist<string> ();
	Private lock lock = new Reentrantlock ();
	Private Condition Notfull = Lock.newcondition ();
	
	Private Condition Notempty = Lock.newcondition ();
	
	private static Writerqueue manager = new Writerqueue ();
	Private Writerqueue () {} public static Writerqueue Getqueue () {return manager;
		
		public void put (String phone) {lock.lock ();
				try {while (queue.size () = = Max_queue_size) {System.out.println ("Warning:data queue is full!");
			Notfull.await ();
			
			Queue.addfirst (phone);
		Notempty.signal ();
		catch (Interruptedexception e) {e.printstacktrace ();
		} finally{Lock.unlock (); }} public LinkedliSt<string> Takeall () {linkedlist<string> retVal = new linkedlist<string> ();
		
		Lock.lock ();
				try {while (queue.size () = = 0) {System.out.println ("Warning:data queue is empty!");
			Notempty.await ();
} retval.addall (queue);
			
			for (String str:queue) {//Retval.add (str);//}//Empty queue queue.clear ();
		Notfull.signal ();
		catch (Interruptedexception e) {e.printstacktrace ();
		} finally{Lock.unlock ();
	return retVal;
 }
}

Writetask_new.java simulate the thread class that produces the data

Package com.yulore.write;

public class Writetask_new implements Runnable {
	

	@Override public
	void Run () {for
		(int i=0;i<20;i++) {
//			try {
//Sleep				(m);			catch (Interruptedexception e) {
//				e.printstacktrace ();
			Writerqueue.getqueue (). Put ("for:" +i+ "Thread:" +thread.currentthread (). GetName ());
		}

	private void sleep (int millis) throws Interruptedexception {
		thread.sleep (Millis);
	}
	
}

Outputtask.java is responsible for writing data to a file

Package com.yulore.write;
Import Java.io.BufferedWriter;
Import Java.io.File;
Import Java.io.FileOutputStream;
Import java.io.IOException;
Import Java.io.OutputStreamWriter;

Import java.util.LinkedList;
	
	public class Outputtask implements Runnable {private String fileName;
	Public Outputtask (String fileName) {this.filename = filename;
			@Override public void Run () {while (true) {try {5000);
			catch (Interruptedexception e) {e.printstacktrace ();
			} linkedlist<string> list = Writerqueue.getqueue (). Takeall ();
			Write2disk (list);
		list = null; }} private void Write2disk (linkedlist<string> list) {if List==null | |
			List.size () ==0) {System.out.println ("no data ...");
		Return
		
		} System.out.println ("Start serialization data" +filename);
		String Path = "d:/fbb/myworkspace_dw07/";
		
		File outputfile = new file (path+filename); if (Outputfile==null | |!
			Outputfile.exists ()) {try {outputfile.createnewfile (); catch (IoexcEption e) {e.printstacktrace ();
		} fileoutputstream out = null;
		OutputStreamWriter writer = null;
		
		BufferedWriter bw = NULL;
			try {out = new FileOutputStream (outputfile, true);
			writer = new OutputStreamWriter (out);
			
			BW = new BufferedWriter (writer);
				for (String content:list) {bw.write (content);
				Bw.newline ();
			Bw.flush ();
		} catch (IOException e) {e.printstacktrace ();
			}finally{try {if (bw!=null) bw.close ();
			catch (IOException e) {e.printstacktrace ();
	}} private void sleep (int millis) throws Interruptedexception {Thread.Sleep (Millis);
 }

}

Test class

Package com.yulore.write;

public class Testwrite {

	/**
	 * @param args
	 *
	/public static void main (string[] args) {
		
//		Test () ;
		test02 ();
	}

	private static void test02 () {
		writetask_new write = New writetask_new ();
		for (int i=0;i<4;i++) {
			new Thread (write). Start ();
		
		Outputtask output = new Outputtask ("Abc.txt");
		New Thread (Output). Start ();

	private static void Test () {
		Writetask write = new Writetask ("Abc.txt");
		for (int i=0;i<5;i++) {
			new Thread (write). Start ();







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.