0% found this document useful (0 votes)
22 views

Chapter 4 Thread

Uploaded by

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

Chapter 4 Thread

Uploaded by

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

Java Programming

Chapter 4.
Java Threads

Transparency No. 1
Java Threads

 Thread Concept in Java


 Before introducing the thread concept, we were unable to run more than one task
in parallel.
 It was a drawback, and to remove that drawback, Thread Concept was
introduced.
 A Thread is a very light-weighted process, or we can say the smallest part of the
process that allows a program to operate more efficiently by running multiple
tasks simultaneously.
 In order to perform complicated tasks in the background, we used the Thread
concept in Java. All the tasks are executed without affecting the main program.
In a program or process, all the threads have their own separate path for
execution, so each thread of a process is independent.

Transparency No. 2
Java Threads
Cont…

 Another benefit of using thread is that if a thread gets an


exception or an error at the time of its execution, it doesn't affect
the execution of the other threads.
 All the threads share a common memory and have their own
stack, local variables and program counter.
 When multiple threads are executed in parallel at the same time,
this process is known as Multithreading.

Transparency No. 3
Java Threads

 Generally, A thread in Java is the direction or path that is taken while a

program is being executed.

 all the programs have at least one thread, known as the main thread, that

is provided by the JVM or Java Virtual Machine at the starting of the

program's execution.

Transparency No. 4
Java Threads

in a simple way, a Thread is a:


•Feature through which we can perform multiple activities
within a single process.
•Lightweight process.
•Series of executed statements.
•Nested sequence of method calls.

Transparency No. 5
Java Threads

Transparency No. 6
Java Threads
Cont…

1) New (Ready to run)


A thread is in New when it gets CPU time.
2) Running
A thread is in a Running state when it is under execution.
3) Suspended
A thread is in the Suspended state when it is temporarily inactive or under
execution.
4) Blocked
A thread is in the Blocked state when it is waiting for resources.
5) Terminated
A thread comes in this state when at any given time, it halts its execution
immediately.

Transparency No. 7
Java Threads

 Creating Thread
 A thread is created either by "creating or implementing"
the Runnable Interface or by extending the Thread class.
These are the only two ways through which we can create a
thread.
 Let's dive into details of both these way of creating a thread:

Transparency No. 8
Java Threads

Thread Class
A Thread class has several methods and constructors which allow us to perform
various operations on a thread. The Thread class extends the Object class.
The Object class implements the Runnable interface. The thread class has the
following constructors that are used to perform various operations.
•Thread()
•Thread(Runnable, String name)
•Thread(Runnable target)
•Thread(ThreadGroup group, Runnable target, String name)
•Thread(ThreadGroup group, Runnable target)
•Thread(ThreadGroup group, String name)
•Thread(ThreadGroup group, Runnable target, String name, long stackSize)

Transparency No. 9
Java Threads

 Runnable Interface(run() method)


 It’s required to be implemented by that class whose instances are intended to be
executed by a thread. The runnable interface gives us the run() method to
perform an action for the thread.
 start() method
 The method is used for starting a thread that we have newly created.
 It starts a new thread with a new callstack. After executing the start() method,
the thread changes the state from New to Runnable.
 It executes the run() method when the thread gets the correct time to execute it.
 Let's take an example to understand how we can create a Java thread by
extending the Thread class:

Transparency No. 10
Java Threads

ThreadExample1.java
// Implementing runnable interface by extending Thread class
public class ThreadExample1 extends Thread {
// run() method to perform action for thread.
public void run()
{
int a= 10;
int b=12;
int result = a+b;
System.out.println("Thread started running..");
System.out.println("Sum of two numbers is: "+ result);
}
public static void main( String args[] )
{
// Creating instance of the class extend Thread class
ThreadExample1 t1 = new ThreadExample1();
//calling start method to execute the run() method of the Thread class
t1.start();
}
}

Transparency No. 11
Java Threads
output

Transparency No. 12
Java Threads

Thread life cycle

Transparency No. 13
Java Threads

Thread Life Cycle


 New state: The thread is considered not alive.

 Runnable (Ready-to-run) state: A thread


start its life. On this state a thread is waiting
for a turn on the processor.

 Running state: the thread is

currently executing

 Dead state: its run() method completes.

 Blocked: is waiting the resources that are hold


by another thread.

Transparency No. 14
Java Threads
Priority

 The priority of a thread tells the scheduler how important this


thread is.

 The thread scheduler can use the thread priorities to determine


the execution schedule of threads.

 Priorities are integer values

 Thread.MIN_PRIORITY: 1
 Thread.MAX_PRIORITY: 10
 Thread.NORM_PRIORITY: 5

Transparency No. 15
Java Threads

Synchronization

 Synchronization in Java is the process that allows only one thread at


a particular time to complete a given task entirely.
 By default, the JVM gives control to all the threads present in the
system to access the shared resource, due to which the system
approaches race condition
 Synchronization in Java is the capability to control the access
of multiple threads to any shared resource.
 Java Synchronization is better option where we want to allow
only one thread to access the shared resource.

Transparency No. 16
Java Threads

Why use Synchronization?


The synchronization is mainly used to
1.To prevent thread interference.
2.To prevent consistency problem.

Transparency No. 17
Java Threads

Transparency No. 18
Java Threads

Chapter 5

Transparency No. 19
Java Threads

Java Remote Object Invocation

• In Java, the object is serialized before being passed as a parameter to an


RMI.
 Platform-dependent objects such as file descriptors and sockets cannot be
serialized.

• During an RMI local objects are passed by object copying whereas remote
objects are passed by reference.
• A remote object is built from two different classes:
 Server class – implementation of server-side code.

 Client class – implementation of a proxy.

Transparency No. 20
Java Threads

•In Java, a proxy is serializable and is used as a


reference to the remote object.
 It is possible to marshal a proxy and send it as a series
of bytes to another process.
 Passing proxies as parameters works because each

process is executing in the same Java virtual


machine.

Transparency No. 21
Java Threads

Remote Method Invocation (RMI)


• The Java Remote Method Invocation (RMI) system allows an object
running in one Java Virtual Machine (VM) to invoke methods on an
object running in another Java VM.
• Java RMI provides applications with transparent and lightweight access
to remote objects.
• RMI defines a high-level protocol and API.
• Programming distributed applications in Java RMI is simple.
 It is a single-language system.

 The programmer of a remote object must consider its behavior in a


concurrent environment.

Transparency No. 22
Java Threads

Java RMI

• A Java RMI application is referred to as a distributed


object application which needs to:
 Locate remote objects: Applications can use one of two
mechanisms to obtain references to remote objects.
 An application can register its remote objects with RMI's simple
naming facility, the rmiregistry, or the application can pass and
return remote object references as part of its normal operation.

Transparency No. 23
Java Threads

 Communicate with remote objects:

Details of communication between remote objects are handled by RMI; to the

programmer, remote communication looks like a standard Java method

invocation.

Load class bytecodes for objects that are passed around:

 Because RMI allows a caller to pass objects to remote objects, RMI

provides the necessary mechanisms for loading an object's code, as well as

for transmitting its data.

Transparency No. 24
Java Threads

Java RMI

• Java RMI extends the Java object model to provide support

for distributed objects in the Java language.

• It allows objects to invoke methods on remote objects using the

same syntax as for local invocations.

• Type checking applies equally to remote invocations as to local

ones.

Transparency No. 25
Java Threads

•The remote invocation is known because

RemoteExceptions has been handled and the remote object

is implemented using the Remote interface.

•The semantics of parameter passing is different from the

local invocation because the invoker and the target reside

on different machines.

Transparency No. 26
Java Threads
RMI Implementation

Client Host Server Host

Java Virtual Machine Java Virtual Machine

Client Remote
Object Object

Stub Skeleton

Transparency No. 27
Java Threads

Remote References and Interfaces

• Remote References
 Refer to remote objects, but can be invoked on a client just
like a local object reference

• Remote Interfaces
 Declare exposed methods like an RPC specification

 Implemented on the client like a proxy for the remote object

Transparency No. 28
Java Threads

Stubs and Skeletons

• Client Stub
 lives on the client

 pretends to be the remote object

• Sever Skeleton
 lives on the server

 receives requests from the stub

 talks to the true remote object

 delivers the response to the stub

Transparency No. 29
Java Threads
Remote Interface

Remote Interface

implements implements

Remote Object
Client Stub Skeleton
(Server)

Transparency No. 30
Java Threads

RMI Implementation

• Reference Layer – determines if referenced object is local or remote.

• Transport Layer – packages remote invocations, dispatches

messages between stub and skeleton, and handles distributed garbage

collection.

• Both layers reside on top of java.net.

Transparency No. 31
Java Threads

RMI Registry

• the RMI registry is a simple server-side bootstrap naming facility that


allows remote clients to get a reference to a remote object.
• Servers name and register their objects to be accessed remotely with
the RMI Registry.
• Clients use the name to find server objects and obtain a remote
reference to those objects from the RMI Registry.
• A registry (using the rmiregistry command) is a separate process
running on the server machine.

Transparency No. 32
Java Threads
RMI Registry Architecture

Java Virtual Machine Java Virtual Machine

Remote
Client
Object

Skeleton Server
Stub

Registry
“Bob”

Java Virtual Machine

Transparency No. 33
Java Threads

Creating Distributed Applications Using RMI

• Steps to create a RMI application:


1. Design and implement the components of your distributed
application.

2. Compile sources and generate stubs.

3. Make classes network accessible.

4. Start the application.

Transparency No. 34
Java Threads

Design and Implement the Application Components

• This step includes:


 Defining the remote interfaces: A remote interface specifies the methods that

can be invoked remotely by a client.

 Implementing the remote objects: Remote objects must implement one or more

remote interfaces.

 The remote object class may include implementations of other interfaces

(either local or remote) and other methods (which are available only locally).

Transparency No. 35
Java Threads

Cont…
Implementing the clients: Clients that use remote
objects can be implemented at any time after the
remote interfaces are defined, including after the
remote objects have been deployed.

Transparency No. 36
Java Threads

Transparency No. 37

You might also like