Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Threading question....

  1. #1
    Junior Member
    Join Date
    Jul 2009
    Location
    SomeWhere in the world
    Posts
    27
    Thanks
    1
    Thanked 11 Times in 6 Posts

    Question Threading question....

    I got this sample from JavaPassion site.It's great one for beginners
    it's very good sample about getting current Threads information
    -----------------------------------------------------------------------------------

    public class DisplayAllThreads {
     
        public static void main(String[] args) {
     
            // Start three threads first.  They should belong
            // to a same ThreadsGroup.
            new SimpleThread("Boston").start();
            new SimpleThread("New York").start();
            new SimpleThread("Seoul").start();
     
            Thread[] tarray = findAllThreads();
     
            for (int i=0; i<tarray.length;i++){
                System.out.println("Thread " + tarray[i].getName()
                + " in thread group " + tarray[i].getThreadGroup().getName());
            }
     
        }
     
        // Create an array of all threads in the system.
        public static Thread[] findAllThreads() {
            ThreadGroup group = Thread.currentThread().getThreadGroup();
     
            ThreadGroup topGroup = group;
     
            while (group != null) {
                topGroup = group;
                group = group.getParent();
            }
     
            int estimatedSize = topGroup.activeCount() * 2;
            Thread[] slackList = new Thread[estimatedSize];
     
            int actualSize = topGroup.enumerate(slackList);
     
            Thread[] list = new Thread[actualSize];
            System.arraycopy(slackList, 0, list, 0, actualSize);
     
            return list;
        }
    }
     
     
    class SimpleThread extends Thread {
     
        public SimpleThread(String str) {
            super(str);
        }
     
        public void run() {
            for (int i = 0; i < 5; i++) {
                // System.out.format("%d %s%n", i, getName());
                try {
                    sleep((long)(Math.random() * 1000));
                } catch (InterruptedException e) {}
            }
            System.out.format("DONE! %s%n", getName());
        }
    }

    All i need to know Why when getting total number of active thread He multiply 2
    this line...

     int estimatedSize = topGroup.activeCount() * 2;


  2. #2
    Super Moderator Json's Avatar
    Join Date
    Jul 2009
    Location
    Warrington, United Kingdom
    Posts
    1,274
    My Mood
    Happy
    Thanks
    70
    Thanked 156 Times in 152 Posts

    Default Re: Threading question....

    There is an array called slackList created which needs to be able to hold the amount of threads needed, therefore since the activeCount() method only returns an estimate he then multiply that estimate by 2 to make sure he can store all the threads in his slackList array. Then he enumerate() through them which will give him the correct number of threads. Then he creates a new list with that correct number of threads and copies the slackList into it and returns it.

    // Json

Similar Threads

  1. Problem in Threading , ThreadGroup
    By neo_2010 in forum Threads
    Replies: 1
    Last Post: August 30th, 2009, 11:19 PM
  2. Regex Question
    By igniteflow in forum Java SE APIs
    Replies: 1
    Last Post: August 28th, 2009, 11:46 AM
  3. [SOLVED] Difference in the code on changing logical operators
    By lotus in forum Loops & Control Statements
    Replies: 3
    Last Post: June 20th, 2009, 03:30 AM
  4. [SOLVED] How to narrow down the range of input in java?
    By big_c in forum What's Wrong With My Code?
    Replies: 5
    Last Post: April 20th, 2009, 11:38 AM
  5. How to show 2D array using combobox and radiobutton?
    By Broken in forum Loops & Control Statements
    Replies: 1
    Last Post: March 10th, 2009, 06:01 AM