求高手解决线程和io流问题(急!!!!!) 生产者从输入文件source.txt中读取字符串(一串数字如:45,54,2,8,65,99,354,5)信息并放入缓冲区,消费者从缓冲区中获取信息字符串输出到结果输出文件result.txt中要求:生产者线程从文件中读取字符串数字并存入缓冲区,缓冲区(Buffer)中允许的最大数字数为5个io Thread 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 抽时间写了一个,看看吧,理解一下。package com.study.test;import java.io.BufferedReader;import java.io.BufferedWriter;import java.io.File;import java.io.FileNotFoundException;import java.io.FileReader;import java.io.FileWriter;import java.io.IOException;import java.util.ArrayList;import java.util.List;public class T { public static void main(String[] args) throws Exception { Share share = new Share(); File in = new File("D:\\1\\resource.txt"); File out = new File("D:\\1\\result.txt"); Reader reader = new Reader(share, in); Writer writer = new Writer(share, out); reader.start(); writer.start(); }}class Share{ private List<String> contents = new ArrayList<String>(5); private boolean isEnd = false; public boolean isEnd() { return isEnd; } public void setEnd(boolean isEnd) { this.isEnd = isEnd; } public synchronized String get() { while (!isEnd && contents.size() == 0) { try { wait(); } catch (InterruptedException e) { } } String line = null; synchronized(contents){ if(contents.size() == 0){ return null; } line = contents.remove(0); } notifyAll(); return line; } public synchronized void put(String value) { while (contents.size() >= 5) { try { wait(); } catch (InterruptedException e) { } } notifyAll(); contents.add(value); }}class Reader extends Thread{ private Share shared; private File file; public Reader(Share shared, File file){ this.shared = shared; this.file = file; } public File getFile() { return file; } public void setFile(File file) { this.file = file; } public void run(){ BufferedReader reader = null; try { reader = new BufferedReader(new FileReader(file)); while(true){ String line = reader.readLine(); if(line == null){ shared.setEnd(true); break; } System.out.println("read line : " + line); shared.put(line); } } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally{ if(reader != null){ try { reader.close(); } catch (IOException e) { e.printStackTrace(); } } } }}class Writer extends Thread{ private Share shared; private File file; public Writer(Share shared, File file){ this.shared = shared; this.file = file; } public File getFile() { return file; } public void setFile(File file) { this.file = file; } public void run(){ BufferedWriter writer = null; try { writer = new BufferedWriter(new FileWriter(file)); while(!shared.isEnd()){ String line = shared.get(); System.out.println("write line : " + line); writer.write(line + "\n"); } while(true){ String line = shared.get(); if(line == null){ System.out.println("已经写完了...."); break; } System.out.println("write line : " + line); writer.write(line); } writer.flush(); } catch (IOException e) { e.printStackTrace(); } finally{ if(writer != null){ try { writer.close(); } catch (IOException e) { e.printStackTrace(); } } } }} 谢谢abstruct!看你写的我很有收获!马上自己写写,再次感谢! 大神帮我看一下这个哪错了 java.lang.NoClassFoundError:javax/mail/Message socket编程问题(server与client之间实现对话)!!!!!!!!!!!!!!!! 请问如何对用户输入的信息进行检查,比如不许信息包括引号或特殊字符等。 异或的问题 问一个运行时错误 一个jtree的问题! 如何根据一个数据库表名取得它的主键和所有其他列名?? 谁能说说jsp中的exception对象的使用方法?100分 :) java 递归计算结果保存在全局变量中.返回后结果丢失 问个java面向对象调用问题 关于GC回收对象问题
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
public class T {
public static void main(String[] args) throws Exception {
Share share = new Share();
File in = new File("D:\\1\\resource.txt");
File out = new File("D:\\1\\result.txt");
Reader reader = new Reader(share, in);
Writer writer = new Writer(share, out);
reader.start();
writer.start();
}}
class Share{
private List<String> contents = new ArrayList<String>(5);
private boolean isEnd = false; public boolean isEnd() {
return isEnd;
} public void setEnd(boolean isEnd) {
this.isEnd = isEnd;
} public synchronized String get() {
while (!isEnd && contents.size() == 0) {
try {
wait();
} catch (InterruptedException e) {
}
}
String line = null;
synchronized(contents){
if(contents.size() == 0){
return null;
}
line = contents.remove(0);
}
notifyAll();
return line;
} public synchronized void put(String value) {
while (contents.size() >= 5) {
try {
wait();
} catch (InterruptedException e) {
}
}
notifyAll();
contents.add(value);
}
}class Reader extends Thread{
private Share shared;
private File file;
public Reader(Share shared, File file){
this.shared = shared;
this.file = file;
}
public File getFile() {
return file;
}
public void setFile(File file) {
this.file = file;
} public void run(){
BufferedReader reader = null;
try {
reader = new BufferedReader(new FileReader(file));
while(true){
String line = reader.readLine();
if(line == null){
shared.setEnd(true);
break;
}
System.out.println("read line : " + line);
shared.put(line);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally{
if(reader != null){
try {
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}class Writer extends Thread{
private Share shared;
private File file;
public Writer(Share shared, File file){
this.shared = shared;
this.file = file;
}
public File getFile() {
return file;
}
public void setFile(File file) {
this.file = file;
}
public void run(){
BufferedWriter writer = null;
try {
writer = new BufferedWriter(new FileWriter(file));
while(!shared.isEnd()){
String line = shared.get();
System.out.println("write line : " + line);
writer.write(line + "\n");
}
while(true){
String line = shared.get();
if(line == null){
System.out.println("已经写完了....");
break;
}
System.out.println("write line : " + line);
writer.write(line);
}
writer.flush();
} catch (IOException e) {
e.printStackTrace();
} finally{
if(writer != null){
try {
writer.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}