• Post Reply Bookmark Topic Watch Topic
  • New Topic

Two questions regarding thread

 
Quazi Irfan
Ranch Hand
Posts: 86
Java Netbeans IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Question 1 : All threads spawned by the instances of Thread are User Threads by default.

Is this statement correct?


Question 2 : Java executed it's program top down. So in the following code...

...it's guaranteed that threadA will always start before threadB. Is that correct?
 
Campbell Ritchie
Marshal
Posts: 52531
119
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You mean as opposed to daemon threads? I think that is right, but not certain.
Why do you think that thread1 would necessarily start before thread2?
 
Paul Clapham
Sheriff
Posts: 21872
36
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Quazi Irfan wrote:Question 2 : Java executed it's program top down. So in the following code...

...it's guaranteed that threadA will always start before threadB. Is that correct?


No, it's guaranteed that the start() method of threadA will be called before the start() method of threadB is called. But there's no guarantee about the order of the asynchronous processing which those methods initiate. In other words, calling the start() method of a Thread object is not identical to starting that thread.
 
Paul Clapham
Sheriff
Posts: 21872
36
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:You mean as opposed to daemon threads? I think that is right, but not certain.


Yes, the API documentation for the setDaemon() method of Thread says:

Marks this thread as either a daemon thread or a user thread.


(I wasn't familiar with that terminology either, which is why I went to look it up.)
 
Quazi Irfan
Ranch Hand
Posts: 86
Java Netbeans IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paul Clapham wrote:
Campbell Ritchie wrote:You mean as opposed to daemon threads? I think that is right, but not certain.


Yes, the API documentation for the setDaemon() method of Thread says:

Marks this thread as either a daemon thread or a user thread.


So every thread I spawn is User Thread by default.

Got it. Thanks.
 
Stevens Miller
Bartender
Posts: 1421
29
C++ Java Netbeans IDE Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paul Clapham wrote:
Quazi Irfan wrote:Question 2 : Java executed it's program top down. So in the following code...

...it's guaranteed that threadA will always start before threadB. Is that correct?


No, it's guaranteed that the start() method of threadA will be called before the start() method of threadB is called.


I'd go a little further with that, Paul, and say that it is guaranteed that ThreadA.start() will return before ThreadB.start() is called. However, it seems likely that Quazi is asking about threads started by those calls and, in that case, you are quite right: there's no way to know which steps in one of those threads will execute prior to, or after, any steps in the other thread. So, while ThreadA.start() will return before ThreadB.start() is called (meaning that ThreadB.start() will not be called until after ThreadA.start() has returned), that tells us nothing about which thread (or threads) created by those calls will begin executing code first. For practical purposes, they can be written in either order, and all other code should not rely on them to start any threads in any particular order.
 
Paul Clapham
Sheriff
Posts: 21872
36
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, that's better than what I wrote. And just to clarify a bit more: The Thread.start() method starts up a thread (not a Java Thread object, it already is one, but a thread in the operating system) and then returns. That thread will first do some operating-system level housekeeping, no doubt, and after that it will set the Java Thread object's state to RUNNABLE. Finally it will call the run() method of the Runnable object associated with the Thread. Somewhere in the process is what Java programmers refer to as "starting" the Thread, but I'm not exactly sure where that would be.

And it's possible for that process to be delayed in favour of running other threads; we aren't allowed to ask what the reasons for that might be or even care about those reasons much because it's not our business, it's the operating system's business.
 
Campbell Ritchie
Marshal
Posts: 52531
119
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Now too difficult a question for “beginning”. Moving discussion.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!