- 221
- 18
- CPU
- intel dual core n3050
- Scheda Madre
- asus x540s
- RAM
- 4gb
- GPU
- intel HD
- OS
- Windows 10
Produttore
Consumatore
Il produttore scrive in un file out.txt la frase "ciao mamma" andando a capo tutte volte per una durata di 1 minuto.
Il consumatore deve essere eseguito MENTRE il produttore produce il file out.txt.
Ecco cosa succede, Il consumatore NON SA mai la la fine del file Quindi legge costantemente la frase "ciao mamma".
Come si fa a sapere quando il produttore ha terminato di scrivere su file e quindi il consumatore deve "rileggere" il file per intero e riprendere la lettura da dove era rimasto?
Potete aiutarmi in questo dubbio, ci sbatto la testa ma.
Java:
package fileproducer;
import java.util.*;
import java.io.*;
import java.nio.file.*;
import java.io.File;
import java.lang.String;
/**
*
* @author zabit
*/
public class FileProducer extends Thread implements Runnable
{
private boolean finish;
private String p;
private int s;
public FileProducer(){
System.out.println("FileProducer constructor.");
this.finish = false;
this.p = new String("/home/zabit/NetBeansProjects/FileProducer/out/out.txt");
this.s = 60000;
}
public void run() {
System.out.println("run method");
try {
Thread.sleep(s);
} catch (InterruptedException e) {
Thread.currentThread().interrupt(); //set the flag back to <code>true
}
System.out.println("Non sono piu in attesa");
this.finish = true;
}
public boolean getFinish(){
return this.finish;
}
public void begin(){
System.out.println("begin method");
try{
System.out.println(p);
FileOutputStream fos = new FileOutputStream(p, true);
while(this.getFinish() == false)
{
byte[] tb = "ciao mamma\n".getBytes();
fos.write(tb);
}
System.out.println("Ho smesso di scrivere.");
fos.close();
}catch(IOException e){
e.printStackTrace();
}
}
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
FileProducer fp = new FileProducer();
fp.start();
fp.begin();
}
}
Java:
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package fileconsumer;
import java.nio.file.*;
import java.lang.String;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.util.Timer;
import java.util.TimerTask;
/**
*
* @author zabit
*/
public class FileConsumer {
private int c;
/**
* @param args
*/
public FileConsumer(){
File file = new File("/home/zabit/NetBeansProjects/FileProducer/out/out.txt");
this.c = 0;
try {
RandomAccessFile r = new RandomAccessFile(file, "r");
//First time read
String str = null;
while((str = r.readLine()) != null) {
System.out.println("str:"+str);
}
r.seek(r.getFilePointer());
startTimer(r);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
FileConsumer fc = new FileConsumer();
}
private void startTimer(final RandomAccessFile r) {
System.out.println("method startTimer");
Timer timer = new Timer();
timer.scheduleAtFixedRate(new TimerTask() {
private int c;
protected void TimerTask(){
this.c = 0;
}
@Override
public void run() {
this.c++;
String str = null;
try {
while((str = r.readLine()) != null) {
System.out.println("c:"+c+"|"+str);
}
r.seek(r.getFilePointer());
} catch (IOException e) {
e.printStackTrace();
}
}
}, 0, 1000);
}
}
Il produttore scrive in un file out.txt la frase "ciao mamma" andando a capo tutte volte per una durata di 1 minuto.
Il consumatore deve essere eseguito MENTRE il produttore produce il file out.txt.
Ecco cosa succede, Il consumatore NON SA mai la la fine del file Quindi legge costantemente la frase "ciao mamma".
Come si fa a sapere quando il produttore ha terminato di scrivere su file e quindi il consumatore deve "rileggere" il file per intero e riprendere la lettura da dove era rimasto?
Potete aiutarmi in questo dubbio, ci sbatto la testa ma.