0

I wrote a small program in java to send and receive a data file using two threads. I want the two threads to be in the same class. One thread sends the file and the other thread receives the file. I have wrote the code for it but with few errors. Can you help me figure out the errors in the code. I am a student and a beginner in java, so spare me if there are any silly mistakes.

import java.lang.Thread.*;
import java.io.*;
public class sendques implements Runnable
{
    int i=0,c;
    static Thread[] t= new Thread[2];
    FileInputStream fis=new FileInputStream("ip.jpg");
    FileOutputStream fos=new FileOutputStream("output.jpg");
    sendques() {
        for(i=0;i<2;i++){
            t[i]=new Thread(this);
            t[i].start();
            System.out.println("Threads "+i);
        }
    }
    void run() {
        while(true) {
            wait();
            send();
        }
    }
    void send() {
        while((c=fis.read())!=-1) {
            t[2].receive(c);
            wait();
        }
    }
    void receive(int d) {
        while(c!=-1) {
            fos.write(d);
            t[1].notify();
        }
    }
    public static void main(String arg[]) {
        sendques sq=new sendques();
        t[1].send();
        System.out.println("Quiting..");
    }
}  
4
  • 1
    What errors are you seeing? Please edit your question to provide the specifics pointing out which line corresponds to which line number. Commented Apr 9, 2012 at 16:57
  • Mistake #1: code does not compile. Unless you use a custom implementation of Thread there is no such thing like send, write or receive. Commented Apr 9, 2012 at 16:59
  • 2
    There are way too many problems to solve in this code. Besides the fact that it does not compile, the design is really not reusable as it is. You should obviously separate the sending methods from the receiving ones in two separate classes. You should also have the Thread creation in a third one or use one of the ThreadPools offered by Java. Try reading documentation about Producer/Consumer and Threading. There are plenty of them on SO Commented Apr 9, 2012 at 17:55
  • @GuillaumePolet +1: This is a classical Producer/Consumer problem from Computer Science. Instead of pizza's, there are byte[] arrays that are passed between the threads. Commented Apr 10, 2012 at 13:28

1 Answer 1

1

Do not use notify, better use notifyAll, since a liveness failure can occur, named: missed signals. It would make a hard time to correct your code, here is the code for an implementation of producer/consumer with different classes:

The Buffer classes are used to store the data shared between producer and consumer. They have their own classes and an example you will find in BoundedBuffer.java. Their is no heavy computing task involved, just passing messages between both. This is a clean implementation, try to work through it.

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.