Experiment 14 - 132
Experiment 14 - 132
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.
Output:
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
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);
}
}
}
}
}
}
}
}
}
Output:
Conclusion: Thus implemented multithreading.