Solved

problem in using the thread when adding object dynamically

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

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
splitOdd10 challenge 5 108
Java DateChooser? 3 36
Basic Java Case or If-Else statement... 3 50
egit plugin on eclipse 8 63
By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
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:
The viewer will learn how to implement Singleton Design Pattern in Java.

777 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