0% found this document useful (0 votes)
11 views52 pages

Unit 4-Multithreading

Uploaded by

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

Unit 4-Multithreading

Uploaded by

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

Mutlithreading

Multitasking
• Process based (multitasking ) – multiple
programs
• Thread based(multi threading ) – single
program
Introduction to multithreading
• Multithreading is a process to execute multiple threads at
the same time without dependency of other threads.
• Multithreading increase the system performance
Program1
Bank
Statement 1 – t1 main()
{
Statement 2 – t2 Statement 1 Deposit()
Statement 3 – t3 Statement 2 Withdraw()
Statement 4 - t4 Statement 3 Display()
Statement 4 CheckBalance()
}
Thread
• Definition: Thread is a predefined class which is available in java.lang
package.
• Thread is a basic unit of CPU and it is well know for independent
execution
program 1
stat-1 – t1- independent execution (memory)(any exception)
stat-2 – t2 - independent execution
(memory)
stat-3 – t3 - independent execution
JVM
(memory)
stat-4 - t4 - independent execution
(memory)
stat-5 – t5- independent execution
(memory)
Difference between process and thread
Process Thread
1. Process is program under execution 1. Thread is a sub part of a process or small
task of a process

t1 t2 t3 t4 t1 t2 t3 t4
p1 p2
2. Heavy weighted process 2. Less weighted thread
3. Context switching 3. All threads will run at the same time
4. More time 4. Less time
5.Address space is different for different 5.Address space is same for all the threads in a
process process

6. Resource consumption is more 6. Resource consumption is less


7. Creation of process will take more time 7. Thread creation will take less time
8. Time for termination of process will be 8. Time for termination of thread is less
more
How to create thread in java
1) by extending Thread class
2) by implementing Runnable interface
Thread class

public class Thread


public class Thread {
{ start()
run() //implicitly called by start() methods
Constructors sleep()
join()
Methods getID()
getName()
} setName()
getPriority()
setPriority()
isAlive()
interrupt()
notify()
wait()
finalize()
}
Runnable interface
public interface Runnable
{
run();
}
1. by extending Thread class

class A extends Thread class B


{ {
@overriding public static void main(String args[])
public void run() {
{ A ob1=new A();
S.o.p(“hello”); ob1.start();
} }
} }

Output:
hello
class MultithreadingDemo extends Thread
{
public void run()
{
System.out.println("My thread is in running state.");
}
public static void main(String args[])
{
MultithreadingDemo obj=new MultithreadingDemo();
obj.start();
}
}
Output:
My thread is in running state.
2. by implementing Runnable interface
class A implements Runnable class B
{ {
public void run() public static void main(String args[])
{ {
S.o.p(“hello”); A ob1=new A();

} Thread t1=new Thread(ob1);


} t1.start();
}
Thread life cycle
class A extends Thread class B
{ {
public void run() public static void main(String args[])
{ {
System.out.println(“hello”); A ob1=new A();
} ob1.start()
} }
}

New or Ready or Running Dead


Born Runnable
state start() run()

Wait
import java.util.*;
class A
{
public static void main(String args[])
{
System.out.println(“hello”);
system.out.println(Thread.cuurentThread().getName());
}
}

Output:
hello
main thread
1. by extending Thread class

class A extends Thread class B


{ {
@overriding public static void main(String args[])
public void run() {
{ A ob1=new A();
S.o.p(“hello”); ob1.start();
ob1.start(); //error
} }
} }
1. by extending Thread class

class A extends Thread class B


{ {
@overriding public static void main(String args[])
public void run() {
{ A ob1=new A();
S.o.p(“hello”); ob1.run();
} A ob2=new A();
} ob2.run();
}
}
1. by extending Thread class

class A extends Thread class B


{ {
@overriding public static void main(String args[])
public void run() {
{ A ob1=new A();
S.o.p(“hello”); ob1.start(); thread 0
} A ob2=new A();
} ob2.run(); thread 1
}
}
Which is the best way to create thread, Using
Thread class or using Implements Runnable class

Answer: using Implements Runnable interface


Four categories
1. Performing single task with single thread
2. Performing single task with multiple threads
3. Performing multiple tasks with single thread
4. Performing multiple tasks with multiple
threads
Performing single task with single thread

class A extends Thread class B


{ {
@overriding public static void main(String args[])
public void run() {
{ A ob1=new A();
S.o.p(“hello”); ob1.start();
} }
} }
Performing single task with multiple
threads

class A extends Thread class B


{ {
public void run() public static void main(String args[])
{ {
S.o.p(“hello”); A ob1=new A(); thread 1
} ob1.start();
} A ob2=new A() thread 2
ob2.start();
A ob3=new A(); thread 3
ob3.start();
}
}
Performing multiple tasks with multiple threads
class Test1 extends Thread class Test3 extends Thread
class Demo
{ {
{
public void run() public void run()
public static void main(String args[])
{ {
{
s.o.p(“task1”); s.o.p(“task3”);
Test1 t1=new Test1();
} }
t1.start();
} }
Test2 t2=new Test2();
class Test2 extends Thread class Test4 extends Thread
t2.start();
{ {
Test1 t3=new Test3();
public void run() public void run()
t3.start();
{ {
Test2 t4=new Test4();
s.o.p(“task2”); s.o.p(“task”);
t4.start();
} }
} }
}
}

Note: totally 5 threads will create and any thread can execute first, it depends on the
jvm(Thread scheduler- FIFO, RoundRobin etc),but all the threads will execute at the same
time , one thread is not depend on the other thread
class A extends Thread
{
public void run()
{
for(int i=1;i<=5;i++)
{
System.out.println(i); Output:
} 1
2
}
3
} 4
class B 5
{
public static void main()
{
A t1=new A();
t1.start();
}
}
class A extends Thread A t3=new A();
{ t3.start();
A t4=new A();
public void run()
t4.start();
{ }
for(int i=1;i<=5;i++) }
{
Output
System.out.println(i); 1
} 1
} 2
2
}
1
class B 1
{ 3
public static void main() 3
2
{ 2
A t1=new A(); Continue….
t1.start();
A t2=new A();
t2.start();
class A extends Thread
getId() method in
{
public void run() Thread class
{
for(int i=1;i<=5;i++)
{
System.out.println(i); Output:
} Id=21
1
}
2
} 3
class B 4
{ 5

public static void main()


{
A t1=new A();
System.out.println(“Id=”+t1.getID());
t1.start();
}
class A extends Thread
getName() method in
{
public void run() Thread class
{
for(int i=1;i<=5;i++)
{
System.out.println(i); Output:
} Name of the thread : thread 0
1
}
2
} 3
class B 4
{ 5

public static void main()


{
A t1=new A();
System.out.println(“name of the thread=”+t1.getName());
t1.start();
}
class A extends Thread
{
setName() method in
public void run() Thread class
{
for(int i=1;i<=5;i++)
{
System.out.println(i);
} Output:
Name of the thread : thread 0
}
Name of the thread : java
} 1
class B 2
{ 3
public static void main() 4
5
{
A t1=new A();
System.out.println(“name of the thread=”+t1.getName());
t1.setName(“java”);
System.out.println(“name of the thread=”+t1.getName());
t1.start();
}
class A extends Thread
{ currentThread() method
public void run() in Thread class
{
for(int i=1;i<=3;i++) Output:
{ name of the thread : thread 0
System.out.println(i); Name of the thread : thread 1
} 1
} 1
2
}
3
class B 2
{ 3
public static void main()
{
A t1=new A();
System.out.println(“name of the thread=”+Thread.currentThread().getName());
A t2=new A();
System.out.println(“name of the thread=”+Thread.currentThread().getName());
t1.start();
t2.start();
}
class A extends Thread Output:
{ 1
public void run() 1
{ 2
for(int i=1;i<=5;i++) 2
{ 3
System.out.println(i); 1
} 1
} 1
} 4
class B Continue…………….
{
public static void main(String args[])
{
A t1=new A();
t1.start();
A t2=new A();
t2.start();
A t3=new A();
t3. start();
}
}
class A extends Thread currentThread() method
{
public void run() in Thread class
{
for(int i=1;i<=5;i++)
{
System.out.println( +i +”:thread ”+Thread.currentThread().getName());
}
}
} Output:
class B 1 : thread 0
{ 1: thread 1
public static void main(String args[]) 2: thread 0
{ 2: thread 1
A t1=new A(); 3: thread 0
t1.start(); 1: thread 3
A t2=new A(); 1: thread 4
t2.start(); 1: thread 5
A t3=new A(); 4: thread 0
t3. start(); continue
}
}
class A extends Thread sleep() method in Thread
{
public void run()
class
{
for(int i=1;i<=5;i++)
{
System.out.println(i);
Thread.sleep(1000); // thread sleep for 1s(milliseconds) or Thread t1=new Thread()
} t1.sleep(1000)
} Output:
} 1
class B 2
{ 1
public static void main(String args[]) 2
{ 1
A t1=new A(); 1
t1.start(); 2
A t2=new A(); 2
t2.start(); 3
A t3=new A(); 3
t3. start(); 2
} 2
} continue
Thread priorities in java
• Every thread have a priority and priority will be assigned by JVM.
• Priorities values from 1 to 10
1-lower priority
5-normal priority
10- maximum priority thread 1( priority 10) Execute first
thread 2( priority 4) Execute third
thread 3( priority 7) Execute second
thread 4 (priority 3) Execute last
JVM

• setPriority() – to set the priority for thread


• getPriority() – to get the priority for thread
class A extends Thread
{
public void run() getPriority() method in Thread
{ class
System.out.println(“Task1”); 1. Total priority will be from 1 to
} 10
} 2. Default priority value is 5
class B
{
public static void main()
{
A t1=new A(); Output:
System.out.println(“priority=”+t1.getPriority()); priority: 5
t1.start(); Task1
}
}
class A extends Thread
{
public void run() getPriority() method and
{ setPriority() in Thread class
System.out.println(“Task1”); 1. Total priority will be from 1 to
} 10
} 2. Default priority value is 5
class B
{
public static void main()
{
A t1=new A();
Output:
System.out.println(“priority=”+t1.getPriority()); priority: 5
t1.setPriority(7); priority: 7
System.out.println(“priority=”+t1.getPriority()); Task1
t1.start();
}
}
class A extends Thread
{
public void run()
{
System.out.println(“Task1”);
}
public static void main(String args[])
{
A t1=new A();
t1.setPriority(7);
t1.start();
A t2=new A();
t2.setPriority(10);
t2.start();
A t3=new A();
t3.setPriority(6);
t3.start();
A t4=new A();
t4.setPriority(1);
t4.start();
}
class A extends Thread
{
public void run()
{
System.out.println(“Task1”);
}
public static void main(String args[])
{
A t1=new A();
t1.start();
A t2=new A();
t2.start();
A t3=new A();
t3.setPriority(6);
t3.start();
A t4=new A();
t4.setPriority(1);
t4.start();
}
class A extends Thread
{ join() method in
public void run() thread class
{
for(inti=1;i<=3;i++)
System.out.println(i);
} Output:
public static void main(String args[]) 1
2
{
3
A t1=new A(); 1
A t2=new A(); 2
A t3=new A(); 3
1
t1.start(); 2
t1.join(); // wait for t1 to complete its execution 3
t2.start();
t2.join(); // wait for t2 to complete its execution
t3.start();
}
class A extends Thread
{ isAlive() method in
public void run() thread class-status
{
for(int i=1;i<=3;i++)
System.out.println(i);
}
public static void main(String args[])
Output:
{ Status:false(thread is not
A t1=new A(); started its execution)
System.out.println(“status:”+t1.isAlive()); 1
t1.start(); 2
3
System.out.println(“status:”+t1.isAlive()); Status:true
}
}
class A extends Thread
{
public void run()
{
System.out.println(“hello”);
}
public static void main(String args[])
{
Output:
Thread t1=new Thread();
t1.start();
}
}
class A extends Thread
{
public void run()
{
System.out.println(“hello”);
}
public static void main(String args[])
{
Output:
A ob2=new A(); hello
Thread t1=new Thread(ob2);
t1.start();
}
}
class A extends Thread
{
public void run()
{
System.out.println(“hello”);
}
public static void main(String args[])
{
Output:
Thread t1=new Thread(new A()); hello
t1.start();
}
}
Write a Java program creating two threads t1 and t2 and fetch the names of
the thread using getName() method.
import java.util.*;
public class Exercise extends Thread
{
public void run( )
{
System.out.println(“Tasks”);
}

public static void main(String[] args)


{
Exercise t1 = new Exercise();
Exercise t2 = new Exercise();
t1.start(); // Thread started
t2.start(); // Thread started
System.out.println("Thread names are following:");
System.out.println(t1.getName());
System.out.println(t2.getName());
}
}
yield() method in thread class

• yield() method stops the current executing


thread and give the chance to other threads for
execution.
A t1=new A();
A t2=new A();
t1.start();
t2.start();
t1.yield(); //pause t2 and allow t1 to complete its
execution
class A extends Thread
{
public void run()
{
for(int i=1;i<=5;i++)
{
System.out.println(+i +”name=“+Thread.currentThread().getName());
}
}
public static void main(String args[]) 1
{ 2
A ob1=new A(); 3
1 name=Thread-0
ob1.start();
2 name=Thread-0
A ob2=new A();
3 name=Thread-0
ob2.start();
4 name=Thread-0
ob2.yield(); 5 name=Thread-0
for(int j=1;j<=3;j++)
{
System.out.println(j);
}
}
Thread synchronization
Synchronization
-Method level synchronization
- Block level synchronization

Note: Multiple threads performing single task


t1
t2

t3 Resource
t4
class Test extends Thread
{
public void run() Output:
{ 1
1
for(int i=1;i<=3;i++)
1
{ 2
System.out.println(i); 3
} 2
3
} Continue….
public static void main(String args[])
{
Test t1=new Test();
Test t2=new Test();
t1.start();
t2.start();
}
}
}
class Test extends Thread
{
synchronized public void run() Output:
{
1
for(int i=1;i<=3;i++)
2
{ 3
System.out.println(i); 1
} 2
3
}
public static void main(String args[])
{
Test t1=new Test();
Test t2=new Test();
t1.start();
t2.start();
}
}
}
class Test extends Thread
{
synchronized public void run() Output:
{
1
for(int i=1;i<=3;i++)
2
{ 3
System.out.println(i); 1
} 2
3
} 1
public static void main(String args[]) 2
{ 3
Test t1=new Test();
Test t2=new Test();
Test t3=new Test();
Test t4=new Test();
t1.start();
t2.start();
t3.start();
}
}
}
Math.random() and Random class
import java.util.*;
class A
{
public static void main(String args[])
{
Output:
double n; 23.6787
n=Math.random();
System.out.println(n);
}
}
}
Math.random() and Random class
import java.util.*;
class A
{
public static void main(String args[])
{
double n;
for(int i=1;i<=100;i++)
{
n=Math.random();
System.out.println(n);
}
}
}
Random class
1. Random class in java.util package
2. Methods are
nextInt()
nextFloat()
nextBoolean()
nextDouble()
Random class
import java.util.Random; import java.util.Random;
class A class A
{ {
public static void main(String args[]) public static void main(String args[])
{ {
Random r1 = new Random(); Random r1 = new Random();

for(int i=1;i<=100;i++) for(int i=1;i<=100;i++)


{ {
n=r1.nextInt(); n=r1.nextInt(1000);
System.out.println(n); System.out.println(n);
} }
} }
} }

You might also like