Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie
Hi there,
There is an issue with role permissions that is being worked on at the moment.
If you are having trouble with access or permissions on regional forums please post here to get access: https://www.boards.ie/discussion/2058365403/you-do-not-have-permission-for-that#latest

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

  • 27-10-2006 1: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 Posts: 2,013 ✭✭✭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