0% found this document useful (0 votes)
57 views5 pages

Experiment 14 - 132

The document describes a Java program that demonstrates multithreading. It defines two thread classes called NewThread4 and ThreadDemo2 that each take a character as a parameter. The main method creates two thread objects passing different characters, and calls their start methods. The output shows the characters and their increments being printed interleaved, demonstrating the concurrent execution of the threads.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
57 views5 pages

Experiment 14 - 132

The document describes a Java program that demonstrates multithreading. It defines two thread classes called NewThread4 and ThreadDemo2 that each take a character as a parameter. The main method creates two thread objects passing different characters, and calls their start methods. The output shows the characters and their increments being printed interleaved, demonstrating the concurrent execution of the threads.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

Name: Jonathan D’souza Roll No.

: 132 PID: 192039 SE CMPN A

Experiment 14: Program to demonstrate multithreading in Java.


Aim: Program to demonstrate multithreading in Java.

Problem Statement: Program to implement multithreading in Java.

Theory: Java provides support for writing Multithreaded programs. A multithreaded program
consists of multiple parts of the same program running concurrently (i.e. simultaneously). Here
each part of the program working simultaneously is called a thread, and each thread can work
independently. Also each thread can be performing different task.
e.g. it is possible that a Java program will start and ask for a name of a file to be sent on the
network.
Once filename is input, a program goes further for next task. The file sending can be done in the
background using a separate thread and the program will still be doing some different task
simultaneously. It is possible that the program is performing some user input/output and the
thread that is sending file on network has finished its task in the background.

Sample Program: // Create multiple threads.

class NewThread4 implements Runnable {


String name; // name of thread
char c;
NewThread4(String threadname,char c) {
name = threadname; this.c=c;
}
public void run() {
try {
for(int i = 5; i > 0; i--) {
//System.out.println(name + ": " + i);
System.out.println(" "+ c++);
Thread.sleep(500);
}
} catch (InterruptedException e) {
System.out.println(name + "Interrupted");
}
System.out.println(name + " exiting.");
}
}
class ThreadDemo2{
public static void main(String args[]) {
Thread t1=new Thread(new NewThread4("One",'1'));
System.out.println("New thread: " + t1);
Thread t2=new Thread(new NewThread4("Two",'A'));
System.out.println("New thread: " + t2);
//Thread t3=new Thread(new NewThread4("Three"));
//System.out.println("New thread: " + t3);
t1.start();
t2.start();
//t3.start();
try {
Thread.sleep(10000);
} catch (InterruptedException e) {
System.out.println("Main thread Interrupted");
}
System.out.println("Main thread exiting.");
}
}

Output:

New thread: Thread[Thread-0,5,main]


New thread: Thread[Thread-1,5,main]
A
1
B
2
C
3
D
4
E
5
Two exiting.
One exiting.
Main thread exiting.

Output :
Write a multithreaded program to create two threads. First thread will calculate the sum of five
integers. Second thread will calculate the factorial of first five integers

Program:
class Sum extends

Thread{ public void run(){

int sum=0;

for(int i=1;i<=5;i++)
{ sum=sum+i;
System.out.println(i+":sum="+sum);
try{
Thread.sleep(1000);
}catch(Exception e)
{ System.out.println(e);
}
}
}
}

class Factorial extends Thread{

public void run(){


int factorial=1;
for(int i=1;i<=5;i++)
{ factorial=factorial*i;
System.out.println(i+"factorial="+factorial
); try{
Thread.sleep(1000);
}catch(Exception e)
{ System.out.println(e);
}

}
}
}

class ThreadDemo{public static void main(String[] args) {

Sum s=new Sum();


Factorial f=new
Factorial(); s.start();
f.start();

}
}

Output:
Conclusion: Thus implemented multithreading.

You might also like