CS8392 Oops Unit 4
CS8392 Oops Unit 4
• New
• Runnable
• Running
• Non-Runnable (Blocked)
• Terminated
1) New
The thread is in new state if you create an instance of Thread class but
before the invocation of start() method.
2) Runnable
The thread is in runnable state after invocation of start() method, but
the thread scheduler has not selected it to be the running thread.
3) Running
The thread is in running state if the thread scheduler has selected it.
4) Non-Runnable (Blocked)
This is the state when the thread is still alive, but is currently not
eligible to run.
5) Terminated
A thread is in terminated or dead state when its run() method exits.
How to create thread
• You can see all the detail by typing the jconsole in the
command prompt.
• The jconsole tool provides information about the loaded
classes, memory usage, running threads etc.
Points to remember for Daemon
Thread in Java
• Mutual Exclusive
Synchronized method.
Synchronized block.
static synchronization.
• Cooperation (Inter-thread communication in java)
Mutual Exclusive
• Mutual Exclusive helps keep threads from
interfering with one another while sharing data.
by synchronized method
by synchronized block
by static synchronization
Concept of Lock in Java
• Synchronization is built around an internal entity
known as the lock or monitor.
• Every object has an lock associated with it. By
convention, a thread that needs consistent access
to an object's fields has to acquire the object's lock
before accessing them, and then release the lock
when it's done with them.
T - Type
E - Element
K - Key
N - Number
V - Value
Generic Method
• Like the generic class, we can create a generic
method that can accept any type of arguments.
• Here, the scope of arguments is limited to the
method where it is declared.
• It allows static as well as non-static methods.
simple example of java generic method to print array elements.
We are using here E to denote the element.
Wildcard in Java Generics
• The ? (question mark) symbol represents the
wildcard element. It means any type. If we write <?
extends Number>, it means any child class of
Number, e.g., Integer, Float, and double.
• Now we can call the method of Number class
through any child class object.
Here,
? is a wildcard character.
extends, is a keyword.
Number, is a class present in java.lang package
• Suppose, we want to write the method for the list of Number and
its subtypes (like Integer, Double).
• Using List<? extends Number> is suitable for a list of type Number
or any of its subclasses whereas List<Number> works with the list
of type Number only.
• So, List<? extends Number> is less restrictive than List<Number>.
Example of Upper Bound Wildcard
Output