Unit 4-Multithreading
Unit 4-Multithreading
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
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();
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
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
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();