java HW 7.2
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java HW 7.2相关的知识,希望对你有一定的参考价值。
package com.gmail.vhrushyn;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
public class MyFileWriter implements Runnable {
private boolean readOrWrite = false;
private File to;
private MyCopyBuffer mcb;
private MyArray buffer;
public MyFileWriter(File to, MyCopyBuffer mcb) {
super();
this.to = to;
this.mcb = mcb;
}
private void MyWrite() throws IOException {
FileOutputStream fos = new FileOutputStream(to);
int previous = 1024;
int count = 0;
while ((buffer = mcb.getBuffer(readOrWrite)).getByteCopy() >= 0) {
mcb.setStartReader(true);
mcb.MyNotify();
// System.out.println("write byCopy = "+ buffer.getByteCopy());
fos.write(buffer.getCopyArray(), 0, buffer.getByteCopy());
count++;
System.out.println(count + " kilobytes writed"); // from
// "+buffer.getName()+"
// ,reader
// "+readOrWrite+"
// byteCopy
// "+buffer.getByteCopy());
buffer.setReadOrWrite(!readOrWrite);
if (previous > buffer.getByteCopy()) {
fos.close();
break;
}
previous = buffer.getByteCopy();
}
fos.close();
System.out.println("end of copying");
}
@Override
public void run() {
try {
MyWrite();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
package com.gmail.vhrushyn;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
public class MyFileReader implements Runnable {
private boolean readOrWrite = true;
private File from;
private MyCopyBuffer mcb;
private MyArray buffer;
public MyFileReader(File from, MyCopyBuffer mcb) {
super();
this.from = from;
this.mcb = mcb;
}
private synchronized void MyRead() throws IOException {
FileInputStream fis = new FileInputStream(from);
while ((buffer = mcb.getBuffer(readOrWrite)).getByteCopy() >= 0) {
buffer.setByteCopy(fis.read(buffer.getCopyArray()));
// System.out.println("reader byte = "+buffer.getByteCopy());
if (buffer.getByteCopy() < 0) {
mcb.MyNotify();
fis.close();
break;
}
// System.out.println("read to "+buffer.getName()+", reader
// "+readOrWrite+", byteCopy "+buffer.getByteCopy());
buffer.setReadOrWrite(!readOrWrite);
mcb.setStartReader(false);
mcb.MyNotify();
mcb.MyWait();
}
// System.out.println("reader end");
fis.close();
}
@Override
public void run() {
try {
MyRead();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
package com.gmail.vhrushyn;
public class MyCopyBuffer {
private MyArray firstArray = new MyArray("First Array", true);
private MyArray secondArray = new MyArray("Second Array", true);
private boolean startReader = false;
public MyCopyBuffer() {
super();
// TODO Auto-generated constructor stub
}
public synchronized boolean isStartReader() {
return startReader;
}
public synchronized void setStartReader(boolean startReader) {
this.startReader = startReader;
}
public synchronized MyArray getBuffer(boolean readOrWrite) {
while (firstArray.isReadOrWrite() == !readOrWrite && secondArray.isReadOrWrite() == !readOrWrite) {
try {
MyNotify();
// System.out.println(Thread.currentThread().getName()+" wait");
wait();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
while (true) {
if (readOrWrite == firstArray.isReadOrWrite()) {
// System.out.println("first array
// "+Thread.currentThread().getName());
notifyAll();
return firstArray;
}
if (readOrWrite == secondArray.isReadOrWrite()) {
// System.out.println("second array");
notifyAll();
return secondArray;
}
}
}
public synchronized void MyNotify() {
// System.out.println(Thread.currentThread().getName()+" notify");
notifyAll();
}
public synchronized void MyWait() {
while (!startReader) {
try {
// System.out.println(Thread.currentThread().getName()+" my
// wait");
wait();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
package com.gmail.vhrushyn;
public class MyArray {
private byte[] copyArray = new byte[1024];
private int byteCopy = 0;
private boolean readOrWrite;
private String name;
public MyArray(String name, boolean readOrWrite) {
super();
this.name = name;
this.readOrWrite = readOrWrite;
}
public synchronized byte[] getCopyArray() {
return copyArray;
}
public synchronized int getByteCopy() {
return byteCopy;
}
public synchronized boolean isReadOrWrite() {
return readOrWrite;
}
public synchronized String getName() {
return name;
}
public synchronized void setCopyArray(byte[] copyArray) {
this.copyArray = copyArray;
}
public synchronized void setByteCopy(int byteCopy) {
this.byteCopy = byteCopy;
}
public synchronized void setReadOrWrite(boolean readOrWrite) {
this.readOrWrite = readOrWrite;
}
public synchronized void setName(String name) {
this.name = name;
}
}
package com.gmail.vhrushyn;
import java.io.File;
import java.io.IOException;
public class Main {
public static void main(String[] args) {
File from = new File("Java OOP (All cource).txt");
File to = new File("COPY_Java OOP (All cource).txt");
try {
to.createNewFile();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
MyCopyBuffer mcb = new MyCopyBuffer();
MyFileReader mfr = new MyFileReader(from, mcb);
MyFileWriter mfw = new MyFileWriter(to, mcb);
Thread tR = new Thread(mfr);
Thread tW = new Thread(mfw);
tR.start();
tW.start();
}
}
以上是关于java HW 7.2的主要内容,如果未能解决你的问题,请参考以下文章