?
Solved

problem in using the thread when adding object dynamically

Posted on 2004-04-08
5
Medium Priority
?
229 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
5 Comments
 
LVL 8

Accepted Solution

by:
william_jwd earned 500 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 500 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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
This video teaches viewers about errors in exception handling.
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.
Suggested Courses

649 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