Solved

problem in using the thread when adding object dynamically

Posted on 2004-04-08
5
217 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

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
hibernate jars 4 59
mysql jsp example issue 32 72
Github api to fetch the number of collaborators in a git repo 1 29
Java Inheritance super keyword use 8 37
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
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:
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

733 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