aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes SYNCHRONIZATION - extend Thread - super() -- effect of using super() to Thread constructor? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "SYNCHRONIZATION - extend Thread - super() -- effect of using super() to Thread constructor?" Watch "SYNCHRONIZATION - extend Thread - super() -- effect of using super() to Thread constructor?" New topic
Author

SYNCHRONIZATION - extend Thread - super() -- effect of using super() to Thread constructor?

Nigel Shrin
Ranch Hand

Joined: May 18, 2009
Posts: 140

Hi, I need help understanding the use of super() on Thread. I'm currently studying SCJP6 primarily using K&B SCJP6 and now doing Whizlabs mock exams.
I have come across the use of super() on a subclass extending Thread. I cannot see any examples of this in the text I have been working from (chap 9),
so its probably not essential for the exam, but I want to do Whizlabs to a good standard before booking the exam.

Does "super(threadName); create a new thread immediately, and would that entered the runnable pool instantly?

ie


As well as searching chapter 9 in K&B, I have not found an example explaining the use of super in this context yet on Sun:
http://java.sun.com/docs/books/tutorial/essential/threads or http://java.sun.com/docs/books/tutorial/essential/concurrency

Please point me in the right direction if you know good explanations or examples of this!

MANY THANKS!


Nigel
Anbarasu Aladiyan
Ranch Hand

Joined: Jun 02, 2009
Posts: 182

I think, this is another way of giving name for the thread. Also it does not alter the state of the program.


A.A.Anbarasu
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9305
    
  17

Nigel Shrin wrote:

This doesn't start any thread. This just creates a new instance of the Thread sub-class and sets its name. You'll have to start the thread yourself...


SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
Nigel Shrin
Ranch Hand

Joined: May 18, 2009
Posts: 140
Hi Ankit & Anbarasu,

It seems to make the new Thread runnable, and can be followed with a start()

Thank you for your help
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9305
    
  17

Nigel Shrin wrote:It seems to make the new Thread runnable, and can be followed with a start()

A thread is said to be runnable after you call its start method and the run method isn't complete. Creating an instance of Thread class doesn't make it runnable...
Nigel Shrin
Ranch Hand

Joined: May 18, 2009
Posts: 140

Hi Ankit, thanks for your reply. I've been reading material on Sun and in reference books to understand this.
I was expecting to find a distinction between runnable and running, but they are the same unreportable status and it is down to the JVM which can be architecture & OS specific in its behaviour.
'getState' is runnable and 'isActive' is true - and that is as much as you can interrogate - is that right? (ie it doesn't prove its actually active at that point in time)

Another question arose from reading the API - when it says "extends Enum<Thread.State>" what is extending, the Thread class extends Enum<Thread.State>?

Looking at the API http://java.sun.com/javase/6/docs/api/, it shows the states as being stored in an enum object Thread.State:

java.lang
Enum Thread.State

java.lang.Object
java.lang.Enum<Thread.State>
java.lang.Thread.State
All Implemented Interfaces:
Serializable, Comparable<Thread.State>
Enclosing class:
Thread
public static enum Thread.State

extends Enum<Thread.State>
A thread state. A thread can be in one of the following states:

NEW
A thread that has not yet started is in this state.
RUNNABLE
A thread executing in the Java virtual machine is in this state.
BLOCKED
A thread that is blocked waiting for a monitor lock is in this state.
WAITING
A thread that is waiting indefinitely for another thread to perform a particular action is in this state.
TIMED_WAITING
A thread that is waiting for another thread to perform an action for up to a specified waiting time is in this state.
TERMINATED
A thread that has exited is in this state.


thanks again, and great scores, I aspire!
Nigel Shrin
Ranch Hand

Joined: May 18, 2009
Posts: 140
Ankit Garg wrote:
Nigel Shrin wrote:It seems to make the new Thread runnable, and can be followed with a start()

A thread is said to be runnable after you call its start method and the run method isn't complete. Creating an instance of Thread class doesn't make it runnable...


So the resultant state of a thread created using super(name) is: API definition: "NEW A thread that has not yet started is in this state."

 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: SYNCHRONIZATION - extend Thread - super() -- effect of using super() to Thread constructor?