UNIT 4 NOTES Oops
UNIT 4 NOTES Oops
In Java, to get the current state of the thread, use Thread.getState() method to get the current
state of the thread. Java provides java.lang.Thread.State class that defines the ENUM constants
for the state of a thread, as a summary of which is given below:
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.
A thread can be in only one state at a given point in time. These states are virtual machine states
which do not reflect any operating system thread states.
Now let us do discuss all the methods of this class are illustrated as follows:
join(long millis) Waits at most millis milliseconds for this thread to die
If this thread was constructed using a separate Runnable run
run() object, then that Runnable object’s run method is called;
otherwise, this method does nothing and returns
setDaemon(boolean
Marks this thread as either a daemon thread or a user thread
on)
setName(String
Changes the name of this thread to be equal to the argument name.
name)
setPriority(int
Changes the priority of this thread
newPriority)
A hint to the scheduler that the current thread is willing to yield its
yield()
current use of a processor
➔Priorities in threads
It is a concept where each thread is having a priority which in layman’s language one can say
every object is having priority here which is represented by numbers ranging from 1 to 10.
• The default priority is set to 5 as excepted.
• Minimum priority is set to 1.
• Maximum priority is set to 10.
Here 3 constants are defined in it namely as follows:
1. public static int NORM_PRIORITY
2. public static int MIN_PRIORITY
3. public static int MAX_PRIORITY
Thread.currentThread().setPriority(10);
}
}
}
class MyThread2 extends Thread
{
Table t;
MyThread2(Table t)
{
this.t=t;
}
public void run()
{
t.printTable(100);
}
}
OUTPUT:-
5
10
15
20
25
100
200
300
400
500
➔Java - Interthread Communication
Interthread communication is important when you develop an application where two or more
threads exchange some information.
There are three simple methods and a little trick which makes thread communication possible. All
the three methods are listed below −
public void wait()
Causes the current thread to wait until another thread invokes the notify().
public void notify()
Wakes up a single thread that is waiting on this object's monitor
public void notifyAll()
Wakes up all the threads that called wait( ) on the same object
Example:-
class Chat
{
boolean flag = false;
System.out.println(msg);
flag = true;
notify();
}
System.out.println(msg);
flag = false;
notify();
}
}
ob1.start();
ob2.start();
}
}
OUTPUT:
Hi,
Hello
How are you ?
I am good, what about you?
I am also doing fine!
Great
➔Daemon Thread
Daemon thread in Java is a low-priority thread that performs background operations such
as garbage collection, finalizer, Action Listeners, Signal dispatches, etc.
Daemon thread in Java is also a service provider thread that helps the user thread. Its life is at the
mercy of user threads; when all user threads expire, JVM immediately terminates this thread.
In simple words, we can say that it provides services to user threads for background-supporting
tasks. Daemon thread in Java has no role in life other than to serve user threads.
➔Java Generics
Java Generics allows us to create a single class, interface, and method that can be used with
different types of data (objects).
This helps us to reuse our code.
Generics Work Only with Reference Types:
When we declare an instance of a generic type, the type argument passed to the type parameter
must be a reference type. We cannot use primitive data types like int, char.
Generic Classes:
A generic class is implemented exactly like a non-generic class. The only difference is that it
contains a type parameter section. There can be more than one type of parameter, separated by a
comma. The classes, which accept one or more parameters, are known as parameterized classes or
parameterized types.
Example:-
class Test<T>
{
T ob;
Test(T obj)
{
ob = obj;
}
public T myfun()
{
return ob;
}
}
class Generic1
{
public static void main(String[] args)
{
System.out.println(ob1.myfun());
System.out.println(ob2.myfun());
}
}
Generic Functions:
We can also write generic functions that can be called with different types of arguments based on
the type of arguments passed to the generic method. The compiler handles each method.
Example:-
class GenericM
{
static <T> void myfun(T ob)
{
System.out.println(ob.getClass().getName());
System.out.println(ob);
}
myfun(11);
myfun("Java");
myfun(1.0);
}
}
Note that, in this context, extends is used in a general sense to mean either “extends” (as in
classes). Also, This specifies that T can only be replaced by superClassName or subclasses of
superClassName. Thus, a superclass defines an inclusive, upper limit.
Example:-
class Bound<T extends A>
{
private T ob;
class A
{
public void displayClass()
{
System.out.println("Inside super class A");
}
}
class B extends A
{
public void displayClass()
{
System.out.println("Inside sub class B");
}
}
class C extends A
{
public void displayClass()
{
System.out.println("Inside sub class C");
}
}
}
}
Multiple Bounds
Java Generics supports multiple bounds also, i.e., In this case, A can be an interface or class. If
A is class, then B and C should be interfaces. We can’t have more than one class in multiple
bounds.
Syntax:
<T extends superClassName & Interface>
A type variable with multiple bounds is a subtype of all the types listed in the bound. If one of the
bounds is a class, it must be specified first.
Example:-
class Bound<T extends A & B>
{
private T ob;
interface B
{
public void displayClass();
}
class A implements B
{
public void displayClass()
{
System.out.println("Inside super class A");
}
}
class C extends A
{
public void displayClass()
{
System.out.println("Inside super class C");
}
}
}
}