ArrayBlockingQueue

xiaoxiao2021-02-28  5

package arrayblockingqunce; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.File; import java.io.FileReader; import java.io.FileWriter; import java.util.concurrent.BlockingQueue; public class TestThread extends Thread { BlockingQueue<File> queue; File ff = new File("c:/usr/zxs_test.txt"); public TestThread(BlockingQueue<File> queue) { this.queue = queue; } public void run(){ File f = queue.poll(); if(null!=f) { System.out.println("取出"+f.getName()); try { FileReader fr = new FileReader(f); BufferedReader br = new BufferedReader(fr); String line =null; FileWriter fw = new FileWriter(ff,true); BufferedWriter bw = new BufferedWriter(fw); while((line = br.readLine())!=null) { bw.write(line+"\r\n"); } System.out.println(); br.close(); bw.flush(); bw.close(); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } } } package arrayblockingqunce; import java.io.BufferedWriter; import java.io.File; import java.io.FileWriter; import java.io.IOException; import java.util.concurrent.ArrayBlockingQueue; import java.util.concurrent.BlockingQueue; public class TestBlockingQuence { public static void main(String[] args) throws IOException { BlockingQueue<File> queue = new ArrayBlockingQueue<File>(32); for(int i =0;i<128;i++) { File f = new File("c:/usr/zxs_test"+i+".txt"); f.createNewFile(); FileWriter fw = new FileWriter(f); BufferedWriter bw = new BufferedWriter(fw); for(int j =0;j<30;j++) { bw.write("hello world,hello world,hello world\r\n"); } bw.flush(); bw.close(); queue.add(f); TestThread tt = new TestThread(queue); tt.start(); System.out.println("加入"+f.getName()); } } }
转载请注明原文地址: https://www.6miu.com/read-1400090.html

最新回复(0)