thread ID setting

Posted on 2014-02-07
Last Modified: 2014-02-13

I am working on below example

I have not seen author setting thread ID anywhere. Where it is getting set.

i see output as below

RunnableJob is being run by thread1 (9)
RunnableJob is being run by Thread-1 (11)
Main thread: main(1)
RunnableJob is being run by thread2 (10)

which line is printing below output
RunnableJob is being run by Thread-1 (11)
please advise
Any links resources ideas highly appreciated. Thanks in advance
Question by:gudii9
  • 3
  • 2
LVL 14

Accepted Solution

CPColin earned 250 total points
ID: 39843344
It's thread3, with the default thread name. The Javadocs for the Thread class mention that:

If a name is not specified when a thread is created, a new name is generated for it.

The end of the tutorial you linked mentions that "Thread-1" is the default name, too.
LVL 26

Assisted Solution

dpearson earned 250 total points
ID: 39843723
Specifically it's being set here in the constructor for Thread:

     * Allocates a new {@code Thread} object. This constructor has the same
     * effect as {@linkplain #Thread(ThreadGroup,Runnable,String) Thread}
     * {@code (null, null, gname)}, where {@code gname} is a newly generated
     * name. Automatically generated names are of the form
     * {@code "Thread-"+}<i>n</i>, where <i>n</i> is an integer.
    public Thread() {
        init(null, null, "Thread-" + nextThreadNum(), 0);

Author Comment

ID: 39851100
It's thread3, with the default thread name

which is Default thread?
I see main thread and thread1 and thread2 in the code. Where from default thread is coming in this example.

Specifically it's being set here in the constructor for Thread:

How to check which specific constructor it is setting from
Please advise
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

LVL 14

Expert Comment

ID: 39851107
which is Default thread?

When I said "default thread name," I meant "the name that is assigned to threads, by default." There is no such thing as a "default thread."

Author Comment

ID: 39851151
Default thread is below thread in the code right?
Thread thread3 = new Thread(runnableJob);

Please advise
LVL 14

Expert Comment

ID: 39851154
There is no such thing as a "default thread."

In that code, that thread is being assigned a default name, because you didn't specify one. That's what dpearson and I were talking about on Friday.

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
topping1 challenge 7 73
groovy example issue 10 90
eclipse with Angularjs2.0 3 17
tomcat administrtor 12 29
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:

920 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now