java多線程讀寫檔案執行個體

來源:互聯網
上載者:User

Java多線程讀檔案:

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多線程讀大檔案 * @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 blocke.printStackTrace();}}public void run() {String line = 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;}}} 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多線程寫檔案:

package com.myjava;import java.io.File;import java.io.FileNotFoundException;import java.io.FileOutputStream;import java.io.IOException;import java.util.concurrent.ConcurrentLinkedQueue;/** * 多線程下寫檔案 *  * @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,"線程"+i+",")).start();//多線程往隊列中寫入資料}new Thread(new DealFile(out,queue)).start();//監聽線程,不斷從queue中讀資料寫入到檔案中try {Thread.sleep(3000);if(!Thread.currentThread().isAlive()){System.out.println("線程已結束");}} catch (InterruptedException e) {// TODO Auto-generated catch blocke.printStackTrace();}} catch (FileNotFoundException e) {e.printStackTrace();}}}/** * 將要寫入檔案的資料存入隊列中 *  * @author Administrator *  */class MyThread implements Runnable {private ConcurrentLinkedQueue<String> queue;private String contents;public MyThread() {}public MyThread(ConcurrentLinkedQueue<String> queue, String contents) {this.queue = queue;this.contents = contents;}@Overridepublic void run() {try {Thread.sleep(100);} catch (InterruptedException e) {// TODO Auto-generated catch blocke.printStackTrace();}queue.add(contents);}}/** * 將隊列中的資料寫入到檔案 * @author Administrator * */class DealFile implements Runnable {private FileOutputStream out;private ConcurrentLinkedQueue<String> queue;public DealFile() {}public DealFile(FileOutputStream out, ConcurrentLinkedQueue<String> queue) {this.out = out;this.queue = queue;}@Overridepublic void run() {synchronized (queue) {while (true) {if (!queue.isEmpty()) {try {out.write(queue.poll().getBytes("UTF-8"));} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}}try {Thread.sleep(100);} catch (InterruptedException e) {e.printStackTrace();}}}}}


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.