1

I am trying to read 5000 integers from a file and write these integers to another file using two threads. Here is my code:

public class data {
    int value;
}
import java.util.*;
import java.io.*;
public class oku implements Runnable {

    data d;
    public oku(data d){
        this.d=d;
    }

    public synchronized void run(){
        File f= new File("/home/ayyuce/Desktop/ali.dat");
        try {
            Scanner sc= new Scanner(f);
            while(sc.hasNextInt()) {
                //synchronized(d) {
                d.value=sc.nextInt();
            }
            //}
            sc.close();
        } catch(Exception ex){ }
    }

}
import java.util.*;
import java.io.*;
public class yaz implements Runnable {
    data d;

    public yaz(data d) {
        this.d=d;
    }

    public synchronized void run() {
        File f= new File("/home/ayyuce/Desktop/veri.dat");
        try {
            PrintWriter p = new PrintWriter(f);
            for(int i=0; i<5000;i++){
                //synchronized(d){
                p.println(d.value);
                //System.out.println(d.value);
            }
            //}
            p.close();
        } catch(Exception ex){ }
    }
}
public class main_class {
    public static void main(String[] args) {
        data d= new data();
        //d.value=100;
        oku o= new oku(d);
        yaz y= new yaz(d);
        Thread t1= new Thread(o);
        Thread t2= new Thread(y);
        t1.start();
        t2.start();
    }
}

I used producer consumer algorithm. It reads from file and writes the integer to the value in the data class and reads from data class, writes to the file. But it does not work correctly. It writes zeros to the file. What is the problem with my code?

Thank you

7
  • synchronized void run makes no sense. Commented May 14, 2018 at 22:12
  • 1
    Don't swallow exceptions. Commented May 14, 2018 at 22:12
  • What's the problem? Commented May 14, 2018 at 22:12
  • 3
    There's no blocking/waiting - the "write" thread can just merrily write what ever it wants when ever it wants without any consideration to what the "reading" thread is doing, also, the "reading" thread has no idea when the "writing" thread has actually taken a value, making it safe for the "reading" thread to read the next value. I think you need to do some more research into "blocking queues" Commented May 14, 2018 at 22:15
  • 1
    As MadProgrammer said, Java providesBlockingQueue<Integer> to help solve this consumer/producer scenario. However, you were close using a more primitive approach: You had (before commenting them out) wrapped synchronized(d) around the code in both the consumer and producer, but you did not include the relevant wait() and notify() methods to co-ordinate a single read, followed by a single write. This primitive approach forces you to lock every single read/write, whereas a blocking queue requires no further synchronization. Commented May 14, 2018 at 23:14

1 Answer 1

1

If it were me I would create a separate object to keep track of the reading and writing to the file.

public class fileHandler{
    File f;
    printWriter p

    public void fileHandler(String filename){
        f = new File(filename);
        p = new printWriter(f);
    }

    public void write(String str){
        synchronized(p){
            p.println(str);
        }
    }
}

and just call write from each thread individually. This way you don't have multiple threads writing to the same file at the same time.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.