Solved

problem in using the thread when adding object dynamically

Posted on 2004-04-08
5
206 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
Comment Utility
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
Comment Utility
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
Comment Utility
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

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

762 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

Need Help in Real-Time?

Connect with top rated Experts

7 Experts available now in Live!

Get 1:1 Help Now