Multithreading
•
Multithreading is a concept where a program is
divided into two or more subprograms, which can be executed at the same time in
parallel. This is also known as multithreading.
•
A thread is similar to a program that has a
single flow of control
•
The process
of executing multiple threads simultaneously is known as multithreading.
Advantages
of Multithreading
- The
main purpose of multithreading is to provide simultaneous execution of two
or more parts of a program to maximum utilize the CPU time
- It
enables programmers to do many things at one time
- They
can make a long program into many threads and execute them in parallel.
Steps
to Create a thread in java
•
create a new class that extends Thread, then
override the run() method and then to create an instance of that class.
•
The run() method is what is executed by
the thread after you call start().
•
Here is an example of creating a Java
Thread subclass:
public class MyClass extends Thread
{
public
void run()
{
System.out.println("MyClass
running");
}
}
To create and start the above thread:
MyClass t1 = new MyClass ();
T1.start();
When the run() method executes it will
print out the text " MyClass
running ".
Example
program to create three threads and display the strings using multithreading
concept
class A extends Thread
{
public
void run()
{
for(int
i=1;i<=5;i++)
System.out.println(“Welcome
to C”);
}
}
class B extends Thread
{
public
void run()
{
for(int
i=1;i<=5;i++)
System.out.println(“Welcome
to C++”);
}
}
class C extends Thread
{
public
void run()
{
for(int
i=1;i<=5;i++)
System.out.println(“Welcome
to Java”);
}
}
class threaddemo
{
public
static void main(String args[])
{
A
a=new A();
a.start();
B
b=new B();
b.start();
C
c=new C();
c.start();
}
}
Example 02:
package com.java.Multi_threading;
// Class that implements Runnable for creating a thread
class Task1 implements Runnable {
public void run() {
for (int i = 1; i <= 5; i++) {
System.out.println("Task 1 - Counting: " + i);
try {
Thread.sleep(500); // Simulating some work with sleep
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
// Another class that implements Runnable for creating a thread
class Task2 implements Runnable {
public void run() {
for (int i = 1; i <= 5; i++) {
System.out.println("Task 2 - Counting: " + i);
try {
Thread.sleep(500); // Simulating some work with sleep
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
// Main class to run the threads
class ThreadDemo {
public static void main(String[] args) {
// Creating instances of Runnable classes
Task1 task1 = new Task1();
Task2 task2 = new Task2();
// Creating threads from Runnable instances
Thread thread1 = new Thread(task1);
Thread thread2 = new Thread(task2);
// Starting the threads
thread1.start();
thread2.start();
// Main thread work
for (int i = 1; i <= 5; i++) {
System.out.println("Main Thread - Counting: " + i);
try {
Thread.sleep(500); // Simulating some work with sleep
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
package com.java.Multi_threading;
class Table{
void printTable(int n){//method not synchronized
for(int i=1;i<=5;i++){
System.out.println(n*i);
try{
Thread.sleep(400);
}catch(Exception e){System.out.println(e);}
}
}
}
class MyThread1 extends Thread{
Table t;
MyThread1(Table t){
this.t=t;
}
public void run(){
t.printTable(5);
}
}
class MyThread2 extends Thread{
Table t;
MyThread2(Table t){
this.t=t;
}
public void run(){
t.printTable(100);
}
}
class TestSynchronization1{
public static void main(String args[]){
Table obj = new Table();//only one object
MyThread1 t1=new MyThread1(obj);
MyThread2 t2=new MyThread2(obj);
t1.start();
t2.start();
}
}
0 Comments