Click here to Skip to main content
15,917,176 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I have to write a java program that could show multithreading (threads working simultaneously) is effective in reducing execution's time.
The thing is I already have a code but I need to use threads in it.
please help rewrite it.

What I have tried:

Java
import java.util.*;
public class JavaOnlineTest {

public static String questions(){
Scanner sc=new Scanner(System.in);
int output=0;
System.out.println("1)Who is the 45th president of USA?");
System.out.println("1.Barrack Obama 2.Hillary Clinton 3.Donald Trump 4.George W Bush");
int answer=sc.nextInt();
if(answer==3)
output+=1;
else
output+=0;
System.out.println("2)Who is the 1st Prime Minister of India?");
System.out.println("1.Subash Chandra Bose 2.Jawahar Lal Nehru 3.Sardar Vallabhai Patel 4.Mahatma Gandhi");
answer=sc.nextInt();
if(answer==2)
output+=1;
else
output+=0;
//proceed for 3rd question here.
System.out.println("Your result out of 10 is "+output);
if(output>=0&&output<5){
System.out.println("You've Failed");
}else if(output>4 && output<8){
System.out.println("Your in B Class");
}else if(output>7 && output<=10){
System.out.println("Your in A Class");
}
System.out.println("Do you want to retake the test");
System.out.println("Type 'Y' or 'N'");
String retaking=sc.next().trim();
return retaking;
}

public static void main(String [] args){

boolean retake;

String retaking;


while(true){
retaking=questions();
if(retaking.equalsIgnoreCase("Y")){
retake=true;


}else if(retaking.equalsIgnoreCase("N")){
System.out.println("Good Luck!...");
retake=false;
retaking="N";
break;

}
}


}
}
Posted
Updated 23-Nov-17 4:23am
v2

There is nothing in that code that threading would do anything with. It's entirely a "menu driven" interaction with a user with little to no processing of anything.

Threading helps in situations where a problem, or work load, is compute-bound or I/O-bound.

A long-running procedure is put on a background thread, freeing up the UI thread to respond to user inputs and updating displays.

Compute-bound problems are where a problem can be broken up into multiple parts that can be solved simultaneously, each part being put on it's own thread. An example would be you have to do a long computation on 1,000,000 different items, each having nothing to do with each other. You can break up the 1,000,000 items into, say, 5 groups and have 5 threads each working on it's own group.

I/O-bound problems are where the code is waiting for a response from a file or request operation, like reading a huge file, waiting for a web request, polling an external device, ... whatever. Basically, the thread is not doing anything but waiting for input. So, you can put the "waiting" part on a separate thread, again, freeing up the UI thread to respond to the user and display.
 
Share this answer
 
 
Share this answer
 
Mutithreading would be ineffective in improving the speed of such a program.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900