Oops Unit-4
Oops Unit-4
Oops Unit-4
OBJECTIVES:
• To develop a java application with threads and
generics classes
OUTCOMES:
• Develop Java applications with threads and
generics classes
UNIT IV
MULTITHREADING AND GENERIC PROGRAMMING
• Video
Examples
• 1. Web browsers can download files while letting you
view web pages. When you download a file in a web
browser, the browser starts a separate thread to handle
the download.
• 2. E-mail programs don’t make you wait for all your
messages to download before you can read the first
message. Instead, these programs use separate threads
to display and download messages.
Examples
• 3. Word processors can print long documents in the
background while you continue to work. These programs
start a separate thread to handle print jobs.
• A thread is lightweight.
E:\javapgm>java SyncThread
5
10
15
20
25
100
200
300
400
500
Inter-thread Communication
• Inter-thread communication is a mechanism in which a thread
is paused running in its critical section and another thread is
allowed to enter (or lock) in the same critical section to be
executed
• It is implemented by following methods of Object class
– wait()
• tells the calling thread to give up the monitor and go to
sleep until some other thread enters the same monitor
and calls notify( ).
– notify()
• wakes up a thread that called wait( ) on the same
object
– notifyAll()
• wakes up all the threads that called wait( ) on the same
object
• One of the threads will be granted access.
Inter-thread Communication
Output:
E:\javapgm>java InterThread
Withdraw Initiated...
Account Balance:10000
Low Balance; Waiting for Deposit...
Deposit Initiated...
Deposit Completed...
Account Balance:20000
Withdraw Completed...
Account Balance:5000
Daemon Threads
• A Daemon thread is a background service thread which
runs as a low priority thread and performs background
operations like garbage collection
• setDaemon() method of the Thread class is used to
mark/set a particular thread as a daemon thread
• When we create a thread in java, by default it’s a user
thread
• When a thread is marked as daemon thread, JVM
doesn’t wait it to finish to terminate the program
• As soon as all the user threads are finished, JVM
terminates the program as well as all the associated
daemon threads
public class DaemonThread extends Thread
{
public void run()
{
if(Thread.currentThread().isDaemon())
{
System.out.println("This is Daemon Thread");
}
else
{
System.out.println("This is User Thread");
}
}
public static void main(String[] args)
{
DaemonThread t1=new DaemonThread();//creating thread
DaemonThread t2=new DaemonThread();
DaemonThread t3=new DaemonThread();
t1.setDaemon(true);//now t1 is daemon thread
t1.start();//starting threads
t2.start(); E:\javapgm>java DaemonThread
t3.start(); This is Daemon Thread
} This is User Thread
} This is User Thread
Thread Groups
• To group multiple threads in a single object
• Syntax:
• Allocates a new Thread object so that it has target as its run object, has the
specified name as its name, and belongs to the thread group referred to
by group.
E:\javapgm>java ThreadGroupDemo
Thread Group Name: Parent ThreadGroup
three
one
two
java.lang.ThreadGroup[name=Parent ThreadGroup,maxpri=10]
Thread[one,5,Parent ThreadGroup]
Thread[two,5,Parent ThreadGroup]
Thread[three,5,Parent ThreadGroup]
Suspending and Resuming Thread
E:\javapgm>java SuspendResume
New Thread One
New Thread Two
Thread One: 5
Thread Two: 5
Thread Two: 4
Suspending thread One
Thread Two: 3
Resuming thread One
Suspending thread Two
Thread One: 4
Thread One: 3
Thread Two: 2
Resuming thread Two
Thread One: 2
Thread Two: 1
Thread One: 1
Thread Two exiting.
Thread One exiting.
Generic Programming
• Generic classes
• generic methods
• Bounded Types
• Restrictions and Limitations
Generic Classes
Generic Methods
Restrictions and Limitations
Bounded Types