Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Nigel Shrin
Ranch Hand
Posts: 140
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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!
 
Anbarasu Aladiyan
Ranch Hand
Posts: 182
Chrome Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think, this is another way of giving name for the thread. Also it does not alter the state of the program.
 
Ankit Garg
Sheriff
Posts: 9509
22
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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...
 
Nigel Shrin
Ranch Hand
Posts: 140
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 9509
22
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 140
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Posts: 140
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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."

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic