Advertisement
Help Keep Boards Alive. Support us by going ad free today. See here: https://subscriptions.boards.ie/.
If we do not hit our goal we will be forced to close the site.

Current status: https://keepboardsalive.com/

Annual subs are best for most impact. If you are still undecided on going Ad Free - you can also donate using the Paypal Donate option. All contribution helps. Thank you.
https://www.boards.ie/group/1878-subscribers-forum

Private Group for paid up members of Boards.ie. Join the club.

Java - Naming an instance of an object using a variable.

  • 27-10-2006 02:25PM
    #1
    Closed Accounts Posts: 4




    Hey I'm having trouble writing a program that runs a number of Threads. I want to pass in the number of threads i want to run as a command line parameter(I dont need help with that part).
    The problem is when im naming the threads i want to use the variable i passed in to name the threads using a for loop.


    This is what works so far but its a fixed number of threads
    (Im just including the main function here):
    **************************************
    public static void main(String[] args)
    {
    Thread t1 = new Looping Thread(1);
    t1.start();
    Thread t2 = new Looping Thread(2);
    t1.start();

    /* t3, t4, t5 etc */
    }
    **************************************

    This is how I would like to be able to do it:
    **************************************

    public static void main(String[] args)
    {
    int counter = 0;
    int maxThreads = 0;

    if (args.length > 0) //Cmd line params(not a problem)
    {
    try
    {
    maxThreads = Integer.parseInt(args[0]);
    } catch (NumberFormatException e) {}
    }


    for(counter = 0; counter < maxThreads; counter++)
    { // Problem!
    Thread t(counter) = new LoopingThread(counter);
    t(counter).start(); // How can I name them using "counter"??
    }
    }

    The above code will not work.:(
    Any help would be greatly appreciated!


Comments

  • Closed Accounts Posts: 67 ✭✭gamblingIRE


    hmm.. try this.. I think you are wanting to store the threads in an array, and call the constructor inside the loop, to create the threads on the fly (since creating the array of threads does not initialise or create the thread objects)...
    am not near a compiler, so be gentle if this is incorrect!

    public static void main(String[] args)
    {

    int counter = 0;
    int maxThreads = 0;
    Thread [] threadsArray;
    boolean error=false;

    if (args.length > 0)
    {


    try
    {

    maxThreads = Integer.parseInt(args[0]);
    threadsArray= new Thread[maxThreads];


    } catch (NumberFormatException e) {error=true;}

    }

    if(!error)
    {
    for(counter = 0; counter < maxThreads; counter++)
    {

    threadsArray = new Thread();
    threadsArray.setName(i); //<- Edit - didn't read the first post properly :-s
    threadsArray.start();

    }
    }

    }


  • Registered Users, Registered Users 2, Paid Member Posts: 2,032 ✭✭✭lynchie


    Use .setName("abcd") on each thread object to name them. Everytime you create a thread, stick it in a List (ArrayList) and when you are looking for a particular thread object, iterate through the list, and query the name on the thread to find the one you are looking for. Or, use a hashtable instead and use the name as the key (providing that all thread names are unique)


  • Closed Accounts Posts: 4 --gally--


    A Thread array, Brilliant!:D


Advertisement