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: Problem in Threading , ThreadGroup

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

    Question Problem in Threading , ThreadGroup

    Class number 1
    class PrintNameThread extends Thread
    {
     
        public PrintNameThread(String name) {
            super(name);
        }
     
        public void run(){
            String name = super.getName();
            for(int x = 0 ; x < 10 ; x++)
            {
                System.out.println(name);
            }
        }
     
    }

    Class number 2
    class PrintNameThreadv2 extends Thread
    {
     
        public PrintNameThreadv2(String name) {
            super(name);
            super.setPriority(Thread.MAX_PRIORITY);
           // super.setDaemon(true);
            super.start();
        }
     
        public void run(){
            String name = super.getName();
            for(int x = 0 ; x < 10 ; x++)
            {
                System.out.println(name);
            }
        }
     
    }

    Class number 3
    class PrintNameThreadv3 implements Runnable
    {
        String name ;
        public PrintNameThreadv3(String name) {
            this.name = name ;
        }
     
        public void run(){
            for(int x = 0 ; x < 10 ; x++)
            {
                System.out.println(this.name);
            }
        }
    }

    Main Class
    public class ThreadGroupDemo01 {
        public static void main(String args[])
        {
            PrintNameThread t1 = new PrintNameThread("System");
            t1.setPriority(Thread.MAX_PRIORITY);
            t1.start();
            PrintNameThreadv2 t2 = new PrintNameThreadv2("Access");
     
            PrintNameThreadv3 t3 = new PrintNameThreadv3("NEO");
            Thread th = new Thread(t3);
            th.start();
     
            //Define an array to store current threads.
            Thread threadArray[];
     
            //Return current  thread-group
            ThreadGroup tg = Thread.currentThread().getThreadGroup();
            int num = tg.activeCount();
            System.out.println("Total running thread are " + num);
     
            threadArray = new Thread[num];
            tg.enumerate(threadArray);
     
            for(int i = 0 ; i < num ; i++)
            {
                System.out.println("Thread name : " + threadArray[i].getName() + " , " +
                                   "Thread priority : " + threadArray[i].getPriority()+ " , " +
                                   "Group name : " + threadArray[i].getThreadGroup().getName());
            }
            /**/
     
     
        }
    }

    I want to ask about why it's print 2 of current running threads.It's have to be 4
    System,Access,Neo,Main
    There is something wrong i didn't knew it...So if any one have can say something about this
    or i misunderstand something.


  2. #2
    Moderators helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Problem in Threading , ThreadGroup

    does your output look something like this?
    System
    Access
    Total running thread are 2
    Thread name : main , Thread priority : 5 , Group name : main
    Thread name : Thread-0 , Thread priority : 5 , Group name : main
    NEO
    I'll explain my output and hopefully you can understand what's going on with your output:

    The first two lines are being printed out first because once these threads have been started, they'll run basically independent of the other threads (their data access may not be independent, but their runtimes are). They reached their print-out statements before the main thread could print out the "Thread name : "... stuff

    The active count has dropped to 2 by the time the active thread number method was called because the first 2 threads have already stopped (reached the end of their run methods).

    NEO was printed last because it's print statement happened to came after the thread info was printed.

    Timing multi-threaded applications is a huge problem, and to be quite honest, i'm not too good at it. I have only basic knowledge of how multi-threading and parallel processing works (less than basic knowledge sometimes).