0% found this document useful (0 votes)
14 views27 pages

AdvancedJavaProgramming-SLIDES01-UNIT1-FP2005-Ver 1.0

This document outlines a 3-day training course on advanced Java programming concepts including multithreading, Java database connectivity, remote method invocation, reflection API, Java beans, and XML parsing. The course will cover these topics through lectures and hands-on sessions. On days 1-3, it will focus on multithreading, JDBC, RMI, reflection, and Java beans. Days 4-5 will involve a group project utilizing these skills. Session topics are described in detail including an introduction to threads and synchronization, the Thread class, Runnable interface, thread states, priorities, time slicing, and inter-thread communication techniques.

Uploaded by

loga prakash
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views27 pages

AdvancedJavaProgramming-SLIDES01-UNIT1-FP2005-Ver 1.0

This document outlines a 3-day training course on advanced Java programming concepts including multithreading, Java database connectivity, remote method invocation, reflection API, Java beans, and XML parsing. The course will cover these topics through lectures and hands-on sessions. On days 1-3, it will focus on multithreading, JDBC, RMI, reflection, and Java beans. Days 4-5 will involve a group project utilizing these skills. Session topics are described in detail including an introduction to threads and synchronization, the Thread class, Runnable interface, thread states, priorities, time slicing, and inter-thread communication techniques.

Uploaded by

loga prakash
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 27

Advanced Java Programming

(J2EE LC)
Unit -1, Multithreading
Course Objective
To introduce

– Concepts of Threads

– Java Database Connectivity

– Remote Method Invocation

– Reflection API

– Introduction to Java Beans

– Eclipse IDE

– XML parsing

ER/CORP/CRS/LA22/003
Copyright © 2005, Infosys 2
Technologies Ltd Version 1.00
Day wise Plan
Day 1
– Multithreading

– Java Database Connectivity

Day 2
– Remote Method Invocation

– Reflection API

– Introduction to Java Beans

Day 3
– XML Parsing and introduction to parsers

ER/CORP/CRS/LA22/003
Copyright © 2005, Infosys 3
Technologies Ltd Version 1.00
Day wise Plan
Day 4 & Day 5
– Project

ER/CORP/CRS/LA22/003
Copyright © 2005, Infosys 4
Technologies Ltd Version 1.00
Session Plan
Multithreading
– Creating and managing threads

– Priority management

– Thread synchronization

– Thread groups and daemon threads

Java Data Base Connectivity


– JDBC API

– Different drivers

– Set up a connection to a database from Java

– Create a database application

ER/CORP/CRS/LA22/003
Copyright © 2005, Infosys 5
Technologies Ltd Version 1.00
What are Threads?

A thread is a single sequential flow of control within a program

Facility to allow multiple activities within a single process

Referred as lightweight process

Each thread has its own program counter, stack and local variables

Threads share memory, heap area, files

ER/CORP/CRS/LA22/003
Copyright © 2005, Infosys 6
Technologies Ltd Version 1.00
Why use Threads?

To perform asynchronous or background processing

Increases the responsiveness of GUI applications

Better utilization of system resources

Simplify program logic when there are multiple independent entities

ER/CORP/CRS/LA22/003
Copyright © 2005, Infosys 7
Technologies Ltd Version 1.00
Creating the Thread
Two ways:

Extending the Thread class

Implementing the Runnable interface

ER/CORP/CRS/LA22/003
Copyright © 2005, Infosys 8
Technologies Ltd Version 1.00
The Thread class
Extend the Thread class

Override the run() method

Create an object of the sub class and call the start method to execute the
Thread

ER/CORP/CRS/LA22/003
Copyright © 2005, Infosys 9
Technologies Ltd Version 1.00
The Thread class

ER/CORP/CRS/LA22/003
Copyright © 2005, Infosys 10
Technologies Ltd Version 1.00
The Runnable Interface
Useful when the class is already extending another class and needs to
implement mutithreading

Need to implement the run() method

Create a thread object (the worker) and give it a Runnable object (the job)

– public Thread(Runnable target);

Start the thread by calling the start() method

ER/CORP/CRS/LA22/003
Copyright © 2005, Infosys 11
Technologies Ltd Version 1.00
Thread States

NEWBORN DEAD
start() run() returns or
thread interrupted

notify(), notifyAll()
WAITING RUNNABLE
Sleep interval
expires
yield() or end schedule
of time slice
SLEEPING sleep() wait()

I/O completion RUNNING


BLOCKED
I/O request

NON-RUNNABLE
ALIVE

ER/CORP/CRS/LA22/003
Copyright © 2005, Infosys 12
Technologies Ltd Version 1.00
Thread scheduling (1 of 2)

There are 2 techniques for thread scheduling


– Pre-emptive and time-sliced

In preemptive scheduling, the thread with a higher priority preempts threads


with lower priority and grabs the CPU

In time-sliced or round robin scheduling, each thread will get some time of the
CPU

ER/CORP/CRS/LA22/003
Copyright © 2005, Infosys 13
Technologies Ltd Version 1.00
Thread scheduling (2 of 2)

Java runtime system’s thread scheduling algorithm is preemptive but it


depends on the implementation

Solaris is preemptive, Macintosh and Windows are time sliced

In theory, a thread with high priority should get more CPU time, but practically
it may depend on the platform

ER/CORP/CRS/LA22/003
Copyright © 2005, Infosys 14
Technologies Ltd Version 1.00
Thread Priorities (1 of 2)

Thread priority can be used by the scheduler to decide which thread is to be


run

The priority of a Thread can be set using the method setPriority()


– t.setPriority(7);

The method getPriority() will return the priority of a Thread


– t.getPriority()

ER/CORP/CRS/LA22/003
Copyright © 2005, Infosys 15
Technologies Ltd Version 1.00
Thread Priorities (2 of 2)

The priority can vary from 1 to 10 or Thread.MIN_PRIORITY to


Thread.MAX_PRIORITY

Normally a Thread will have the priority 5 or Thread.NORM_PRIORITY

ER/CORP/CRS/LA22/003
Copyright © 2005, Infosys 16
Technologies Ltd Version 1.00
Ensuring time-slicing (1 of 2)

The programmer should write code to ensure that time-slicing occurs even
when the application is running in a preemptive environment

The static method sleep of the Thread class will make a thread sleep for
specified number of milliseconds
Thread.sleep(1000);

A sleeping thread can be interrupted by another thread using the method


interrupt()

When interrupted, sleep method will throw an InterruptedException

ER/CORP/CRS/LA22/003
Copyright © 2005, Infosys 17
Technologies Ltd Version 1.00
Ensuring time-slicing (2 of 2)

The yield method of the class Thread will give a chance to other threads to run
– t.yield();

The yield() method ensures that the thread behaves like a polite thread and not
a selfish thread

ER/CORP/CRS/LA22/003
Copyright © 2005, Infosys 18
Technologies Ltd Version 1.00
isAlive() and join() methods

The state of a thread can be queried using the isAlive() method


– Returns true if the thread has been started but not completed its task

– A thread can wait for another thread to finish by using the join() method

ER/CORP/CRS/LA22/003
Copyright © 2005, Infosys 19
Technologies Ltd Version 1.00
Thread synchronization

In a multithreaded environment 2 or more threads may access a shared


resource

There should be some means to ensure that the shared resource is accessed
by only one thread at a time. This is termed as synchronization

Every object has a mutually exclusive lock called monitor

Only one thread can get the monitor of an object at a time

ER/CORP/CRS/LA22/003
Copyright © 2005, Infosys 20
Technologies Ltd Version 1.00
Thread synchronization

Synchronization can be ensured by using the keyword synchronized

A method or block of code can be made synchronized

Example
public class Account{

int bankBalance;

public synchronized void creditAccount (int amount) {

bankBalance+= amount;

ER/CORP/CRS/LA22/003
Copyright © 2005, Infosys 21
Technologies Ltd Version 1.00
Inter-thread communication (1 of 3)

If a thread must wait for the state of an object to change while executing a
synchronized method, it may call wait()
– void wait()

– void wait (long timeout)

The thread calling wait will release the lock on that particular object

The thread will wait till it is notified by another thread owning the lock on the
same object
– void notify()

– void notifyAll()

ER/CORP/CRS/LA22/003
Copyright © 2005, Infosys 22
Technologies Ltd Version 1.00
Inter-thread communication (2 of 3)

If there are multiple threads waiting on the same object, the notify() method will
notify one among them

The thread that would be notified cannot be predicted

It is safer to call notifyAll(), since it will notify all waiting threads

Code for synchronization should be written carefully else may lead to a dead-
lock situation

ER/CORP/CRS/LA22/003
Copyright © 2005, Infosys 23
Technologies Ltd Version 1.00
Inter-thread communication (3 of 3)
Example
public class Account {

int bankBalance;
bankBalance;
public synchronized void debitAccount (int amount) {
while((bankBalance - amount)<0) wait();
bankBalance -= amount;

}
public synchronized void creditAccount (int amount) {
bankBalance += amount;
notify();

}
}

ER/CORP/CRS/LA22/003
Copyright © 2005, Infosys 24
Technologies Ltd Version 1.00
Thread Groups
Represents a set of threads

Can also contain other thread groups, creating a hierarchy of thread groups

Provides a single-point control on the threads belonging to the thread group

Creation time association is for the life time of the thread


– ThreadGroup threadGroup = new ThreadGroup("MyGroup");

Some of the important methods of ThreadGroup are


– activeCount(), destroy(), setMaxPriority(), setDaemon()

ER/CORP/CRS/LA22/003
Copyright © 2005, Infosys 25
Technologies Ltd Version 1.00
Daemon Thread

Daemon Thread is a thread that will run in the background, serving other
threads

So, a program can stop, if all non-daemon threads have finished execution

A thread can be made a daemon by calling the method setDaemon(true)


before calling its start() method

Can query thread status using the method isDaemon()

ER/CORP/CRS/LA22/003
Copyright © 2005, Infosys 26
Technologies Ltd Version 1.00
Summary

Multithreading

– Creating and managing threads

– Priority management

– Thread synchronization

– Thread groups and daemon threads

ER/CORP/CRS/LA22/003
Copyright © 2005, Infosys 27
Technologies Ltd Version 1.00

You might also like