1 Reply Latest reply: Oct 31, 2007 12:09 PM by 807600 RSS

    Synchronizing and Coordinating Threads - need help with task !

    807600
      Task description_

      Thread A:_

      - Reads input data from external file such way:

      id_package <whitespace> weight (where id_package is some id of type String and weight would be integer or double)

      (the file should contain a large number of records of at least 10000)

      - Creates objects of class Package (id_package, weight)

      - Prints message in console notifying packages were created (every 200 packages)

      Thread B:_

      - Accesses those created objects (in some way) and sums the weight of those objects;

      - Prints message in console notifying weight was counted and summed (every 100 packages)

      - At the end displays the total weight of all packages



      My problem:_:

      1) How to store information about all the objects - do I need another class or just an array of objects of class Package ?

      2) Which methods should be synchronized and coordinated ?

      I've managed so far:_

      1) define class Package (yay !!)
      2) create a program for generating data source file (the one with id_package and weight)
      3) create Thread A (READER)

      code (not done yet):
      public class ReadTowary extends Thread {
           private BufferedReader readSource;
           
           public ReadTowary(String readFrom) throws FileNotFoundException
           {
                this.readSource = new BufferedReader(new FileReader(readFrom));
           }
           
           public void run()
           {
                
                String line = new String();
                try {
                          while((line = readSource.readLine()) != null)
                          {
                               String[] separator = line.split(" ");
                               Towar t = new Towar(separator[0] , Integer.parseInt(separator[1]));
                               // do something with the created object
                          }
                } catch (NumberFormatException e) {
                
                     e.printStackTrace();
                } catch (IOException e) {
                     // TODO Auto-generated catch block
                     e.printStackTrace();
                }
                
           }
      }
      It would be great if you guys could point me in the right directions...

      Edited by: pimpcane on Oct 31, 2007 9:50 AM
        • 1. Re: Synchronizing and Coordinating Threads - need help with task !
          807600
          Don't extend Thread, implement Runnable instead (see http://forum.java.sun.com/ann.jspa?annID=9)

          I don't know what your question is. I don't see any actual threading here. If you have no idea of threading at all then read this http://java.sun.com/docs/books/tutorial/essential/concurrency/index.html

          If you do have a specific question then ask it. Am I going in the right direction is not a specific question, especially when your code doesn't do anything with threading yet.