Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
100% found this document useful (3 votes)
1K views

All Units Java Handwritten

- Multithreading allows a program to split into multiple threads that can run concurrently by performing different tasks simultaneously. This improves performance. - There are two approaches to creating threads - implementing the Runnable interface or extending the Thread class. Both involve overriding the run() method. - Important thread methods include start() to initiate execution, join() to wait for a thread to finish, and sleep() to pause a thread for a period. isAlive() checks if a thread is still running. - Synchronization is needed when multiple threads access shared resources to prevent race conditions. The synchronized keyword locks an object to allow only one thread access at a time

Uploaded by

pankaj patil
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
100% found this document useful (3 votes)
1K views

All Units Java Handwritten

- Multithreading allows a program to split into multiple threads that can run concurrently by performing different tasks simultaneously. This improves performance. - There are two approaches to creating threads - implementing the Runnable interface or extending the Thread class. Both involve overriding the run() method. - Important thread methods include start() to initiate execution, join() to wait for a thread to finish, and sleep() to pause a thread for a period. isAlive() checks if a thread is still running. - Synchronization is needed when multiple threads access shared resources to prevent race conditions. The synchronized keyword locks an object to allow only one thread access at a time

Uploaded by

pankaj patil
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 195

Scanned by CamScanner

Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Multithreading

Multithreading is a conceptual programming concept where a program (process) is divided


into two or more subprograms (process), which can be implemented at the same time in
parallel. A multithreaded program contains two or more parts that can run concurrently. Each
part of such a program is called a thread, and each thread defines a separate path of
execution.

A process consists of the memory space allocated by the operating system that can contain
one or more threads. A thread cannot exist on its own; it must be a part of a process. There
are two distinct types of Multitasking i.e. Processor-Based and Thread-Based multitasking.

Q: What is the difference between thread-based and process-based multitasking?

Ans: As both are types of multitasking there is very basic difference between the two.
Process-Based multitasking is a feature that allows your computer to run two or more
programs concurrently. For example you can listen to music and at the same time chat with
your friends on Facebook using browser. In Thread-based multitasking, thread is the smallest
unit of code, which means a single program can perform two or more tasks simultaneously.
For example a text editor can print and at the same time you can edit text provided that those
two tasks are perform by separate threads.

Q: Why multitasking thread requires less overhead than multitasking processor?

Ans: A multitasking thread requires less overhead than multitasking processor because of the
following reasons:

Processes are heavyweight tasks where threads are lightweight

 Processes require their own separate address space where threads share the address

 space Interprocess communication is expensive and limited where Interthread

 communication is inexpensive, and context switching from one thread to the next is lower
in cost.

Benefits of Multithreading

1. Enables programmers to do multiple things at one time.


2. Programmers can divide a long program into threads and execute them in parallel
which eventually increases the speed of the program execution
3. Improved performance and concurrency
4. Simultaneous access to multiple applications
Life Cycle of Thread
A thread can be in any of the five following states

1. Newborn State: When a thread object is created a new thread is born and said to be in
Newborn state.
2. Runnable State: If a thread is in this state it means that the thread is ready for execution
and waiting for the availability of the processor. If all threads in queue are of same priority
then they are given time slots for execution in round robin fashion
3. Running State: It means that the processor has given its time to the thread for execution.

A thread keeps running until the following conditions occurs


a. Thread give up its control on its own and it can happen in the following situations
i. A thread gets suspended using suspend() method which can only be revived
with resume() method
ii. A thread is made to sleep for a specified period of time using sleep(time)
method, where time in milliseconds
iii. A thread is made to wait for some event to occur using wait () method. In
this case a thread can be scheduled to run again using notify () method.

b. A thread is pre-empted by a higher priority thread

4. Blocked State: If a thread is prevented from entering into runnable state and subsequently
running state, then a thread is said to be in Blocked state.

5. Dead State: A runnable thread enters the Dead or terminated state when it completes its
task or otherwise terminates.
Main Thread: Every time a Java program starts up, one thread begins running which is
called as the main thread of the program because it is the one that is executed when your
program begins.

Child threads are produced from main thread


 Often it is the last thread to finish execution as it performs various shut down operations
- Creating a Thread Java defines two ways in which this can be accomplished: You can
implement the Runnable interface.
 You can extend the Thread class, itself.
 Create Thread by Implementing Runnable

The easiest way to create a thread is to create a class that implements the Runnable interface.
To implement Runnable, a class need only implement a single method called run( ), which is
declared like this:

public void run( )

You will define the code that constitutes the new thread inside run() method. It is important
to understand that run() can call other methods, use other classes, and declare variables, just
like the main thread can.
After you create a class that implements Runnable, you will instantiate an object of type
Thread from within that class.
Thread defines several constructors.
The one that we will use is shown here:

Thread(Runnable threadOb, String threadName);

Here threadOb is an instance of a class that implements the Runnable interface and the name
of the new thread is specified by threadName. After the new thread is created, it will not start
running until you call its start( ) method, which is declared within Thread.
The start( ) method is shown here:

void start( );

Example to Create a Thread using Runnable Interface


Create Thread by Extending Thread
The second way to create a thread is to create a new class that extends Thread, and then to
create an instance of that class. The extending class must override the run( ) method, which is
the entry point for the new thread. It must also call start( ) to begin execution of the new
thread.

Example to Create a Thread by Extending Thread Class


Q: Can we start a thread twice?
Ans: No, if a thread is started it can never be started again, if you do so, an illegal Thread
State Exception is thrown. Example is shown below in which a same thread is coded to start
again
Important Thread Methods

Thread Priority

Every Java thread has a priority that helps the operating system determine the order in which
threads are scheduled. Java priorities are in the range between MIN_PRIORITY (a constant
of 1) and MAX_PRIORITY (a constant of 10). By default, every thread is given priority
NORM_PRIORITY (a constant of 5).
Threads with higher priority are more important to a program and should be allocated
processor time before lower-priority threads. However, thread priorities cannot guarantee the
order in which threads execute and very much platform dependent.

Example

Use of isAlive() and join() method

The java.lang.Thread.isAlive() method tests if this thread is alive. A thread is alive if it has
been started and has not yet died. Following is the declaration for java.lang.Thread.isAlive()
method

public final boolean isAlive()

This method returns true if this thread is alive, false otherwise.

join() method waits for a thread to die. It causes the currently thread to stop executing until
the thread it joins with completes its task.
Example
Synchronization

When two or more threads need access to a shared resource, they need some way to ensure
that the resource will be used by only one thread at a time. The process by which this
synchronization is achieved is called thread synchronization. The synchronized keyword in
Java creates a block of code referred to as a critical section. Every Java object with a critical
section of code gets a lock associated with the object. To enter a critical section, a thread
needs to obtain the corresponding object's lock.
synchronized(object) { // statements to be synchronized }

Problem without using Synchronization

In the following example method updatesum() is not synchronized and access by both the
threads simultaneously which results in inconsistent output. Making a method synchronized,
Java creates a “monitor” and hands it over to the thread that calls the method first time. As
long as the thread holds the monitor, no other thread can enter the synchronized section of
the code. Writing the method as synchronized will make one thread enter the method and till
execution is not complete no other thread can get access to the method.
Interthread Communication

It is all about making synchronized threads communicate with each other. It is a mechanism
in which a thread is paused running in its critical section and another thread is allowed to
enter in the same critical section to be executed. It is implemented by the following methods
of Object Class:
wait( ): This method 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( ): This method wakes up the first thread that called wait( ) on the same object.
notifyAll( ): This method wakes up all the threads that called wait( ) on the same object. The
highest priority thread will run first. These methods are implemented as final methods in
Object, so all classes have them. All three methods can be called only from within a
synchronized context.
Example
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner

You might also like