Java multithreaded read-write file instance

Source: Internet
Author: User
Tags mongodb postgresql redis


Java multithreaded Read file:





Package Com.myjava;
Import Java.io.BufferedReader;
Import java.io.FileNotFoundException;
Import Java.io.FileReader;
Import java.io.IOException;
Import java.util.ArrayList;
Import java.util.List; public class Threadreaddemo {/**java multi-threaded read large file * @param args/public static void main (string[] args) {Thread t1=
		New Thread (New multithread (), "A");
		Thread T2=new Thread (new multithread (), "B");
		T1.start ();
	T2.start ();
	} class Multithread implements runnable{private static bufferedreader br = NULL;
	
	Private list<string> List;
		static{try {br = new BufferedReader (New FileReader ("F://threaddemo.txt"), 10);
		catch (FileNotFoundException e) {//TODO auto-generated catch block E.printstacktrace ();
		} public void Run () {String = null;
		int count = 0;
			while (true) {//system.out.println (Thread.CurrentThread (). GetName ());
			This.list = new arraylist<string> (); Synchronized (BR) {try {while (line = Br.readline ())!= null) {if (count<15) {list.add (line);
						count++;
							}else {list.add (line);
							Count = 0;
						Break
				A catch (IOException e) {e.printstacktrace ());
				} try {Thread.Sleep (1);
			Display (this.list);
			catch (Interruptedexception e) {e.printstacktrace ();
		} if (line = = null) break;
		} public void display (List<string> List) {for (String str:list) {System.out.println (str);
	} System.out.println (List.size ()); }
	
}

Java multithreaded write file:




Package Com.myjava;
Import Java.io.File;
Import java.io.FileNotFoundException;
Import Java.io.FileOutputStream;
Import java.io.IOException;

Import Java.util.concurrent.ConcurrentLinkedQueue;
		/** * Multi-threaded Write file * * @author Administrator * */public class Threaddemo {public static void main (string[] args) {
		File File=new file ("F:" +file.separator+ "ThreadDemo.txt");
			try {fileoutputstream out=new fileoutputstream (file, true);
			Concurrentlinkedqueue<string> queue=new concurrentlinkedqueue<string> (); for (int i=0;i<10;i++) {new Thread (new Mythread (queue, Thread +i+ ",")). Start ()//multithreading writes data to the queue new thread (new Deal
				File (Out,queue)). Start ()//listener thread, continuously read data from queue to write to file try {thread.sleep (3000); if (!
				Thread.CurrentThread (). IsAlive ()) {System.out.println ("thread closed");
			The catch (Interruptedexception e) {//TODO auto-generated catch block E.printstacktrace ();
		} catch (FileNotFoundException e) {e.printstacktrace ();
		
}	}/** * The data to be written to the file is stored in the queue * * @author Administrator */class Mythread implements Runnable {private concurrentl
	Inkedqueue<string> queue;

	Private String contents; Public Mythread () {} public mythread (concurrentlinkedqueue<string> queue, String contents) {This.queue = Queu
		E
	this.contents = contents;
		@Override public void Run () {try {thread.sleep (100);
		catch (Interruptedexception e) {//TODO auto-generated catch block E.printstacktrace ();
	} queue.add (contents); /** * Writes data from the queue to the file * @author Administrator */class Dealfile implements Runnable {private FileOutputStream out
	;

	Private concurrentlinkedqueue<string> queue; Public Dealfile () {} public dealfile (FileOutputStream out, concurrentlinkedqueue<string> queue) {this.out = O
		Ut
	This.queue = queue;
						@Override public void Run () {synchronized (\) {while (true) {if (!queue.isempty ()) {try { Out.write (Queue.poll (). GetBytes ("UTF-8"));
					catch (IOException e) {//TODO auto-generated catch block E.printstacktrace ();
				} try {thread.sleep (100);
				catch (Interruptedexception e) {e.printstacktrace (); }
			}
		}

	}

}


Alibaba Cloud Hot Products

Elastic Compute Service (ECS) Dedicated Host (DDH) ApsaraDB RDS for MySQL (RDS) ApsaraDB for PolarDB(PolarDB) AnalyticDB for PostgreSQL (ADB for PG)
AnalyticDB for MySQL(ADB for MySQL) Data Transmission Service (DTS) Server Load Balancer (SLB) Global Accelerator (GA) Cloud Enterprise Network (CEN)
Object Storage Service (OSS) Content Delivery Network (CDN) Short Message Service (SMS) Container Service for Kubernetes (ACK) Data Lake Analytics (DLA)

ApsaraDB for Redis (Redis)

ApsaraDB for MongoDB (MongoDB) NAT Gateway VPN Gateway Cloud Firewall
Anti-DDoS Web Application Firewall (WAF) Log Service DataWorks MaxCompute
Elastic MapReduce (EMR) Elasticsearch

Alibaba Cloud Free Trail

Related Article

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.