Professional Documents
Culture Documents
Cloud Lab 4
Cloud Lab 4
Cloud Lab 4
Introduction
Multithreading in Java is a process of executing multiple threads simultaneously.
Multithreading makes multitasking possible when it breaks programs into smaller,
executable threads. Each thread has the programming elements needed to execute the
main program, and the computer executes each thread one at a time. Threads can be
created by using two mechanisms :
Multithreading in cloud computing serves several purposes and can be beneficial for various
aspects of cloud-based applications and services. Purpose of using Multithreading in cloud
computing:
1. Improved Performance
2. Scalability
3. Resource Efficiency
4. Parallel Processing
Program
An example that creates a new thread implementing Runnable interface and starts running
it.
public class lab4 implements Runnable {
private Thread t;
private String threadName;
lab4(String name){
threadName = name;
System.out.println("Creating "+ threadName );
}
public void run() {
System.out.println("Running " + threadName );
try {
for(int i = 4; i > 0; i--) {
System.out.println("Thread: " + threadName + " , " + i);
// Let the thread sleep for a while.
Thread.sleep(50);
}
} catch(InterruptedException e) {
System.out.println("Thread " + threadName + " interrupted.");
}
System.out.println(" Thread " + threadName + " exiting.");
}
public void start () {
System.out.println("Starting " + threadName );
if (t == null) {
t = new Thread (this, threadName);
t.start ();
}
}
}
A thread instance can also be created using the Runnable interface. To create a thread
instance, the class whose objects should be executed by a thread should implement the
Runnable interface. From above program the output has been observed for multithreading.