Problem with thread, interrupt and synchronism [closed]

1

The class Main does this:

    synThread tm = new synThread();
    Person objPersont = new Person();

    //input_file is used to read bytes from an input file
    FileInputStream input_file = new FileInputStream(input_file_path);;

    //buffReader stores data from the input_file
    BufferedReader buffReader = new BufferedReader(new InputStreamReader(input_file));        

    //start thread responsible for reading the input file
    objInput = new DiskInputThread(input_file_path,tm,input_file,buffReader);           
    Thread threadInput = new Thread(objInput);        
    threadInput.start();

    //start thread responsible for processing data retrieved from the input file
    objProcess = new ProcessingThread(tm,buffReader);        
    Thread threadProcess = new Thread(objProcess);   
    threadProcess.start();            

    //input_file is used to read bytes from an input file
    File output_file = new File(output_file_path);        

    //bw writes data into the output file
    FileWriter fw = new FileWriter(output_file.getAbsoluteFile());                
    BufferedWriter bw = new BufferedWriter(fw);

    //if output file doesn't exists, create it
    if (!output_file.exists()) {
        output_file.createNewFile();
    }

    //start thread responsible for writing the output file
    objOutput = new DiskOutputThread(output_file_path,tm,bw,buffReader);           
    Thread threadOutput = new Thread(objOutput);
    threadOutput.start();'

Within Main , it creates an object of class ProcessingThread , which does this:

public class ProcessingThread implements Runnable{   
    private synThread tm;
    private BufferedReader buffReader;

    //set the value received by the constructor
    ProcessingThread(synThread tm_input, BufferedReader buffReader) {
        this.tm = tm_input;
        this.buffReader = buffReader;
    }

    @Override
    public void run() {                      
        //try to process data retrieved from input file
        try {
            while (buffReader.ready()) {
                tm.processInput();
            }
        } catch (IOException ex) {
            Logger.getLogger(ProcessingThread.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}

The processingThread , runs within the synThread class, which does this:

/*
 * 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 agile_t;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import static java.lang.System.exit;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author jimmystocco
 */
public class synThread {    
    private Person objT;
    private boolean available_input = false;      

    public synchronized void readInput(BufferedReader buffReader) {
        while(available_input == true) {
            try {
                wait();
            } catch (InterruptedException e) {

            }
        }
        try {    
            //reading each line of the input file                           
            String line = buffReader.readLine();                

            //separating fields by character ','
            String[] vData = line.split(",");
            String name = vData[0];
            Integer age = Integer.parseInt(vData[1]);
            objT = new Person(age,name);                   
            available_input = true;
            notifyAll();
        } catch (IOException e) {
            System.out.println("Error: " + e);
        }        
    }

    public synchronized void processInput() {
        Integer age;
        age = objT.getAge();

        while(available_input == false) {
            try {
                wait();
            } catch (InterruptedException e) { }
        }

        if (age <= 17) {
            objT.setAgeBand("17");
        }        
        else if(age >= 18 && age <= 24) {
            objT.setAgeBand("18-24");
        }
        else if(age >= 25 && age <= 30) {
            objT.setAgeBand("25-30");
        }
        else if(age >= 31 && age <= 40) {
            objT.setAgeBand("31-40");
        }
        else if(age >= 41 && age <= 55) {
            objT.setAgeBand("41-55");
        }
        else if(age >= 56 && age <= 70) {
            objT.setAgeBand("56-70");
        }
        else if(age > 71) {
            objT.setAgeBand("71+");
        }

        available_input = false;
        notifyAll();
    }

    public synchronized void writeOutput(BufferedWriter bw) {  
        while(objT.getAgeBand() == null) {
            try {
                wait();
            } catch (InterruptedException e) { }
        }
        try {                        
            bw.write(objT.getName());
            bw.write(",");
            bw.write(Integer.toString(objT.getAge()));
            bw.write(",");
            bw.write(objT.getAgeBand());
            bw.write("\n"); 
        }
        catch (IOException e) {
            Logger.getLogger(DiskOutputThread.class.getName()).log(Level.SEVERE, null, e);
        }
    }
}

I have to read the input files, process them, and write to an output file. The reading of an object is done, eg (Andre,26) . The processing directs the ageband of that input (in this case, 25-30), and then it is written to the output file. Problem: The ageband of an entry is being written to the entry of another age due to a timing problem.

    
asked by anonymous 23.11.2014 / 22:58

0 answers