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 (); }
}
}
}
}