Solved

problem in using the thread when adding object dynamically

Posted on 2004-04-08
5
214 Views
Last Modified: 2010-03-31
Hi all,
i have a good question here.
I have a class 'Feeder'(1) which inplements 'Runnable'(2).
There are two methods in (1) they are add(), and other is run(); There is a vector named 'allBufers"
the add method adds an object(which contains another vector, in vector contains 1000 elements) into vector 'allBufers' .
the run method(which runs continuosly by taking 1st object, 2nd obect and ..till all the objects in vector and again starts from the first after completing all the objects.) takes each object from 'allBufers' and processes the vector present in object. The objects are added dynamically into vector 'allBufers'.
The problem is once i run the method 'run' and add an object to vector 'allBufers' it is not recognising that. I dont know if there is flaw in my understading or implementation.

here is the snipplet of code.

package streammanager;
import java.util.*;
import java.io.*;
/**
* Title:
* Description:
* Copyright:    Copyright (c) 2004
* Company:
* @author
* @version 1.0
*/

public class Feeder implements Runnable
{

  Vector allBufers=new Vector();
  public void add(FeederComparable toFeed)
  {
    allBufers.add(toFeed);
  }
  public void run()
  {
    while(true)
    {
        for(int i=0;i<allBufers.size();i++)
        {
           FeederComparable fc=(FeederComparable)allBufers.get(i);
           for(int i=0;i<fc.vct.size();i++)
           {
                 System.out.println( (String)fc.vct.get(i) )
           }//end of for
       }//end of another for
    }//end of while
 }//end of run
}//end of class

this is the test which runs this.

public class TestFeeder
{

  public TestFeeder()
  {
  }
  public static void main(String args[])
  {
    FeederComparable fc1=new FeederComparable(b1,filename1,inputrate1);
    FeederComparable fc2=new FeederComparable(b2,filename2,inputrate2);
    FeederComparable fc3=new FeederComparable(b3,filename3,inputrate3);
    FeederComparable fc4=new FeederComparable(b4,filename4,inputrate4);
    Feeder f=new Feeder();
    f.add(fc1);
    f.run();
    f.add(fc2);
    f.add(fc3);
    f.add(fc4);
  }
}

int the TestFeeder after adding the first object the run method displays all the content of vector in first object.
but i am not able to add anymore objects to the vector.

I think you got the question clear. If you have any problem in undertanding this question let me know.

regards.
vamshi krishna pajjuri
0
Comment
Question by:vamshicheruku
5 Comments
 
LVL 8

Accepted Solution

by:
william_jwd earned 125 total points
ID: 10784324
try this,

package streammanager;
import java.util.*;
import java.io.*;
/**
* Title:
* Description:
* Copyright:    Copyright (c) 2004
* Company:
* @author
* @version 1.0
*/

public class Feeder implements Runnable
{

  Vector allBufers=new Vector();
  public void add(FeederComparable toFeed)
  {
    allBufers.add(toFeed);
  }
  public void run()
  {
    while(true)
    {
        try {
        for(int i=0;i<allBufers.size();i++)
        {
           FeederComparable fc=(FeederComparable)allBufers.get(i);
           for(int i=0;i<fc.vct.size();i++)
           {
                 System.out.println( (String)fc.vct.get(i) )
           }//end of for
       }//end of another for
       Thread.sleep(1000);
       } catch (Exception e) {
       }
    }//end of while
 }//end of run
}//end of class

this is the test which runs this.

public class TestFeeder
{

  public TestFeeder()
  {
  }
  public static void main(String args[])
  {
    FeederComparable fc1=new FeederComparable(b1,filename1,inputrate1);
    FeederComparable fc2=new FeederComparable(b2,filename2,inputrate2);
    FeederComparable fc3=new FeederComparable(b3,filename3,inputrate3);
    FeederComparable fc4=new FeederComparable(b4,filename4,inputrate4);
    Feeder f=new Feeder();
    f.add(fc1);
    f.start();
    f.add(fc2);
    f.add(fc3);
    f.add(fc4);
  }
}
0
 
LVL 7

Assisted Solution

by:searlas
searlas earned 125 total points
ID: 10784467
You forgot to start a separate thread, the problem lies in here:
 public static void main(String args[])
  {
    FeederComparable fc1=new FeederComparable(b1,filename1,inputrate1);
    FeederComparable fc2=new FeederComparable(b2,filename2,inputrate2);
    FeederComparable fc3=new FeederComparable(b3,filename3,inputrate3);
    FeederComparable fc4=new FeederComparable(b4,filename4,inputrate4);
    Feeder f=new Feeder();
    f.add(fc1);
    f.run();
    f.add(fc2);
    f.add(fc3);
    f.add(fc4);
  }

When you call f.run() it gets caught in the while (true) loop forever.
What you probably intended to do was:

 public static void main(String args[]) throws Exception
  {
    FeederComparable fc1=new FeederComparable(b1,filename1,inputrate1);
    FeederComparable fc2=new FeederComparable(b2,filename2,inputrate2);
    FeederComparable fc3=new FeederComparable(b3,filename3,inputrate3);
    FeederComparable fc4=new FeederComparable(b4,filename4,inputrate4);
    Feeder f=new Feeder();
    f.add(fc1);
    new Thread(f).start(); // f.run();
    f.add(fc2);
    f.add(fc3);
    f.add(fc4);
  }
0
 

Author Comment

by:vamshicheruku
ID: 10784575
hey guys,

thanks for your quick reply, i will surely try what you have said and let you know if it works.

regards,
vamshi krishna pajjuri
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

For customizing the look of your lightweight component and making it look lucid like it was made of glass. Or: how to make your component more Apple-ish ;) This tip assumes your component to be of rectangular shape and completely opaque. (COD…
For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:

839 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question