Multiple threads may either increase or decrease the run tim

Multiple threads may either increase or decrease the run time of a particular program depending on

the program needs. Give an example where they would decrease the run time and explain why the run time would decrease. Give an example where they would increase the run time and explain why the run time would increase.

Solution

Answer :

Multithreading :

1) Multithreading concept will support multiple languages.

2) If your computer have one single CPU, you might be wondering how it can execute more than one thread at the same time.In single systems it works only one thread of execution occurs at a given instant.

3) one processor systems support logical concurrency not in physical concurrency.

4) Logical concurrency it\'s execute multiple threads at a time of the execution process.

5) Multithreading is the logically concurrent execution of multiple programs.

Example :

class Multithreading
{
public static void main(String args[])
{
MultiThread threadexample1 = new MultiThread(\"threadexample1: \");
MultiThread threadexample2 = new MultiThread(\"threadexample2: \");
threadexample1.start();
threadexample2.start();
do {
if (thread1IsAlive && !thread1.isAlive()) {
thread1IsAlive = false;
System.out.println(\"Thread 1 is dead.\");
}
if (thread2IsAlive && !thread2.isAlive()) {
thread2IsAlive = false;
System.out.println(\"Thread 2 is dead.\");
}
} while(thread1IsAlive || thread2IsAlive);
}
}

class MultiThread extends Thread
{
static String message[] = { \"Java\", \".NET\", \"PHP,\", \"Oracle,\", \"SQL\", \"MYSQL\"};

public MultiThread(String id)
{
super(id);
}

public void run()
{
String name = getName();
for (int i=0;i<message.length;++i) {
randomWaitstate();
System.out.println(name + message[i]);
}
}

void randomWaitstate()
{
try {
sleep((long)(3000*Math.random()));
} catch (InterruptedException x) {
System.out.println(\"Interrupted!\");
}
}
}

Multiple threads may either increase or decrease the run time of a particular program depending on the program needs. Give an example where they would decrease
Multiple threads may either increase or decrease the run time of a particular program depending on the program needs. Give an example where they would decrease

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site