Question
Week013 - Threads Laboratory Exercise 012 Objective/s: At the end of this activity, you should be able to: Apply the concepts learned in multithreading Learn
Week013 - Threads
Laboratory Exercise 012
Objective/s:
At the end of this activity, you should be able to:
Apply the concepts learned in multithreading
Learn how to join threads.
What to Prepare for the Activity:
NetBeans IDE 8.2
JDK8 (Java Development Kit 8)
Procedure:
Create a NetBeans project for this activity. The project name should be as follows:
Project Name: Lab012_
Example: Lab012_Blanco_Maria
Compress the NetBeans project into .rar or .zip format and then upload to the link provided in the LMS.
Only NetBeans project compressed in .rar or .zip format will be accepted.
Consider the following codes:
public class TestThread extends Thread {
public static void main(String[] args) {
TestThread thread = new TestThread();
}
@Override
public void run() {
printMyName();
}
private void printMyName() {
System.out.println("Thread is running");
}
}
Test Stem / Question | Choices |
1: What method should you invoke to start the thread TestThread? | A: start() |
B: run() | |
C: No. Thread will run automatically when executed. | |
D: TestThread is not a thread. | |
2: What will happen when you started the thread? | A: Thread is running will be printed out. |
B: Will have an compiler error, you cannot access a static method in the thread. | |
C: Exception will be thrown. | |
D: Thread will be started but the Thread is running will not be printed since the run method is not called. | |
3: How many and what threads is/are running when you successfully run the above code? | A: 2, the main thread and the TestThread. |
B: 1, only the main thread | |
C: 1, only the TestThread | |
D: No thread is running | |
4: What method is internally called by when we call the method start()? | A: run() |
B: printMyName() | |
C: main() | |
D: None | |
5: Now try to use the method getName() and print it out. What does the console say? | A: Thread-0 |
B: The name of the class - TestThread | |
C: None since we did not define a name for the thread. | |
D: Syntax error, not such method exists. |
public class TestThread implements Runnable{
public static void main(String[] args){
TestThread t = new TestThread();
}
public void run() {
System.out.println("Running");
}
}
6: How will you be able to run this thread? | A: by calling the run() method. |
B: By calling the start() method. | |
C: This will not run since this is a runnable. You need to extend the Thread class. | |
D: This will not run since there is no @override annotation in the run method. | |
7: What will happen when you called the t.start() twice? | A: IllegalStateThreadException will occur. |
B: The Running text will be printed twice | |
C: Nothing. | |
D: MulitThreadingException will occur | |
8: What will happen when you called t.run() and t.start()? | A: The Running text will be printed twice |
B: IllegalStateThreadException will occur. | |
C: Nothing. | |
D: MulitThreadingException will occur |
public class TestExample extends Thread{
public static void main(String argv[]){
TestExample t = new TestExample();
t.run();
}
public void start(){
for(int i = 0; i < 5; i++){
System.out.println("Count : " + i);
}
}
}
9: What happens when you compile? | A: Nothing but it compiled. |
B: Syntax error. run() method is not defined. | |
C: Compiler error. | |
D: A run time exception that says no run method is defined in this class. |
public class TestExample implements Runnable{
public static void main(String[] args) throws InterruptedException{
Thread a = new Thread(new TestExample());
a.start();
System.out.println("Starting...");
a.join();
System.out.println("Ending...");
}
public void run(){
System.out.println("Running...");
}
}
10: What happens when you compile? | A: Starting Running Ending is printed |
B: Starting Ending Running is printed | |
C: Starting Ending is printed | |
D: Runtime exception occurred. |
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started