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

Oopj Unit4

The document discusses multithreading in Java. It defines processes and threads, with threads being lighter weight than processes. It describes the lifecycle of a thread and how to create threads by extending the Thread class or implementing the Runnable interface. Methods to assign priorities to threads and different types of threads like user threads and daemon threads are also discussed. The document provides examples of creating and running multiple threads concurrently in Java.

Uploaded by

RK HUSSAIN
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)
23 views

Oopj Unit4

The document discusses multithreading in Java. It defines processes and threads, with threads being lighter weight than processes. It describes the lifecycle of a thread and how to create threads by extending the Thread class or implementing the Runnable interface. Methods to assign priorities to threads and different types of threads like user threads and daemon threads are also discussed. The document provides examples of creating and running multiple threads concurrently in Java.

Uploaded by

RK HUSSAIN
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/ 28

MULTITHREADING

Process: A program in execution Is called as a process.


Processes are of two types They are,
1.Heavy-weight process
2.Light-weight process
Heavy-weight process:
A process which requires more memory and more CPU time is called Heavy-weight process.
Light-weight process:
A process which requires less memory and less CPU time is called Light-weight process.
Thread:
A thread is a light-weight process.
(or)
A thread is the path followed when executing a program.
(or)
A thread is a single sequential flow of control in a program

Lifecycle of a thread:
A thread can be in one of the following states
1.Newborn state
2.Runnable state
3.Running state
4.Blocked state
1.Newborn state
When an object of the thread class is created then it is said to be a New born state.
2.Runnable state
When the thread is in a queue waiting for the cpu then it is said to be in Runnable state.
3.Running state
When the thread is being executed by the cpu then it is said to be Running state.
4. Blocked state
A thread enters into blocked state if one of the following methods are called
1.sleep ()
2.wait ()
3.suspend ()
5.Dead state
Once the code of the thread is executed then it enters into dead state from the blocked state if a
thread wants to enter into running state then the following methods are called
1.notify ()
2.resume ()
Creation of threads:
A thread can be created using two methods
1.By extending the thread class
2.By implementing the Runnable interface
Assining priorities to threads:
Set priority () method is used to assign priority to the threads.
MIN-PRIORITY-1
NORM- PRIORITY-5 constants
MAX- PRIORITY-10
Types of threads:
Threads are classified into two types
1.user threads
2.Daemon threads
User threads
The threads which are created by the user (or)programmer are called user threads.
Daemon threads
The threads which are executing in the background process are called Daemon threads.

// Java program for creating multiple threads using thread class.

Class A extends Thread


{
Public void run ()
{
For (int i=1; i<=5; i=i+1)
{
System.out.print (“\n\n\t i=” +i);
}
}
}
Class B extends Thread
{
Public void run ()
{
For (int j=1! j<=5; j=j+1)
{
System.out.print ("\n\n\t j= "+j);
}
}
}
Class C extends Thread
{
Public void run ()
{
For (int k=1; k=5; k=k+1)
{
System.out.print ("\n\n\t k "+k);
}
}
}
Class Demo1
{
Public static void main (String args[])throwsException
{
Aa=new A ();
Bb=new B ();
Cc=new C ();
a.start ();
b.start ();
c.start ();
}
}

// Java program for creating multiple threads using runnable interface

class A implements Runnable


{
public void run ()
{
for (int i=1; i<=s; i="+i);
{
System.out.print("\n\n\t i= "+i);
}
}
}
class B implements Runnable
{
Public void run ()
{
For (int j=1; j<=5; j=j+1)
{
System.out.print('\n\n\t j="+j);
}
}
}
Class C implements Runnable
{
Public void run ()
{
For (int k=1; k<=5; k=k+1)
{
System.out.print("\n\n\t k="+k);
}
}
}
Class Demo2
{
Public static void main (String args[])throwsException
{
A a= new A ();
B b= new B ();
C c= new C ();
Thread t1=new thread (a);
Thread t2=new thread (b);
Thread t3=new thread (c);
T1.start ();
t2.start ();
t3.start ();
}
}

//Java program for assigning priorities to threads

Class A extends Thread


{
Public void run ()
{
For (int i=1; i<=s; i=i+1)
{
System.out.print(\n\n\t i= "+i);
}
}
}
Class B extends Thread
{
Public void run ()
{
For (int j=1; j<=s; j=j+1);
{
System.out.print (\n\n\t j= "+j);
}
}
}
Class C extends thread
{
Public void run ()
{
For (int k=1; k<=s; k=k+1)
{
System.out.print("\n\n\t k= "+k);
}
}
}
Class Demo3
{
Public static void main (String args[])throwsException
{
A a= new A ();
B b= new B ();
C c= new C ();
a.set priority (Thread.MAX_PRIORITY);
b.set priority (Thread.NORM_PRIORITY);
c.set priority (Thread.MIN_PRIORITY);
a.start ();
b.start ();
c.start ();
}

INTER THREAD COMMUNICATION:


The communication between threads is known as Inter thread communication.

SYNCHRONIZATION:
Synchronization refers to the co-operation and co-ordination among multiple processes in
accessing a single resource.
Synchronisation can be achieved in java using three methods:
1.synchronized block
2.synchronized method
3.static block.
1. synchronized block:
synchronised
{

}
2. synchronized method:
public synchronised void run ()
{

}
3. static block:
static
{

}
CLASSICAL PROBLEMS OF SYNCHRONISATION:
1.producer-consumer problem
2.Reader- writers problem
3.Dining philosopher's problem

PRODUCER -CONSUMER PROBLEM:


1.A producer is a process which produces items.
2.A consumer is a process which consumes items.
3.A buffer is a common area shared by both the producer and consumer.

The size of the buffer is limited to one item.


When the buffer is empty,the producer has to produce an item and place it in the buffer.
When the buffer is full,the consumer has to consume the item is placed in the buffer.
Both processes should access buffer without conflict and this is achieved through semaphore.
A semaphore is a binary variable.
A semaphore is associated with the buffer and if the value of the semaphore is zero then it
indicates that the buffer is empty and producer has to act. If the value of the semaphore is one
then it indicates that the buffer is full and consumer has to act
//Java program for executing multiple threads simultaneously

Class Sample 1 executive Thread


{
Public void run ()
{
While(true)
{
System.out.print("\n\n\t GOOD MORNING");
try
{
Sleep (1000);
}
Catch (Exception e1)
{
System.out.print(e1);
}
}
}
}
Class Sample 2 extends Thread
{
Public void run ()
{
While(true)
{
System.out.print("\n\n\t HELLO");
try
{
Sleep (2000);
}
Catch (Exception e2)
{
System.out.print(e2);
}
}
}

}
Class Sample 3 extends Thread
{
Public void run ()
{
While(true)
{
System.out.print("\n\n\t WELCOME");
try
{
Sleep (3000);
}
Catch (Exception e3)
{
System.out.print(e3);
}
}
}
}
Class Sampledemo
{
Public static void main (String args[]) throws Exception
{
Sample s1= new sample1();
Sample s2=new sample2();
Sample s3=new sample3();
s1.start ();
s2.start ();
s3.start ();
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy