7

I am currently learning basics of Threads in Java and I am trying to write a simple Thread Group program. I wrote it same as tutorial website though i'm getting different type of output. Below is my code for which i'm getting different output.

public class ThreadGroupDemo implements Runnable {

    @Override
    public void run() {
        System.out.println(Thread.currentThread().getName());
        // get the name of the current thread.
    }

    public static void main(String[] args) {
        ThreadGroupDemo runnable = new ThreadGroupDemo();
        ThreadGroup tg1 = new ThreadGroup("Parent Group");
        // Creating thread Group.

        Thread t1 = new Thread(tg1, new ThreadGroupDemo(), "one");
        t1.start();
        t1.setPriority(Thread.MAX_PRIORITY);

        Thread t2 = new Thread(tg1, new ThreadGroupDemo(), "second");
        t2.start();
        t2.setPriority(Thread.NORM_PRIORITY);

        Thread t3 = new Thread(tg1, new ThreadGroupDemo(), "Three");
        t3.start();

        System.out.println("Thread Group name : " + tg1.getName());
        tg1.list();
    }

}

I am getting Output :

Thread Group name : Parent Group
Three
java.lang.ThreadGroup[name=Parent Group,maxpri=10]
    second
one
Thread[one,10,Parent Group]
    Thread[second,5,Parent Group]
    Thread[Three,5,Parent Group]

The output should be like :

one
two
three
Thread Group Name: Parent ThreadGroup
java.lang.ThreadGroup[name=Parent ThreadGroup,maxpri=10]
    Thread[one,5,Parent ThreadGroup]
    Thread[two,5,Parent ThreadGroup]
    Thread[three,5,Parent ThreadGroup] 

i am not able to understand why this happening? setting Priority can help with it?

Jaimin Patel
  • 4,559
  • 3
  • 32
  • 35
  • The answer is simple: the tutorial is wrong. The only thing that is actually guaranteed is that the thread group name will appear before the thread group list. – biziclop Feb 01 '16 at 10:32
  • 5
    http://www.javatpoint.com/threadgroup-in-java – Jaimin Patel Feb 01 '16 at 10:33
  • 4
    It's shocking. I advise you to use the [official tutorial](https://docs.oracle.com/javase/tutorial/essential/concurrency/) instead. And forget about [thread groups](http://stackoverflow.com/questions/3265640/why-threadgroup-is-being-criticised), they're fairly useless anyway. And so is setting the priority of a thread. – biziclop Feb 01 '16 at 10:34
  • 1
    Possible duplicate of [Java multithreading - thread priority](http://stackoverflow.com/questions/20333150/java-multithreading-thread-priority) – Petter Friberg Feb 01 '16 at 15:06

1 Answers1

3

You can't predict the order of executions of your threads even with a level of priority. You have no control on the scheduling. It's your OS which decides.

A good book about concurrency in Java : Java concurrency in practice

Patrick
  • 831
  • 11
  • 25
  • To add to that, even home computers now have multiple cores, capable of actually running multiple threads in parallel. Which makes it more likely that a newly started thread will be scheduled to run straight away. – biziclop Feb 01 '16 at 11:13