I am trying to understand java threads but couldn\'t solve this question on the
ID: 3573686 • Letter: I
Question
I am trying to understand java threads but couldn't solve this question on the exercises. could you please answer and provide an explanation?
For the following Java class, choose the correct thread class: public class Test12 { public static void main(String[] args) { Thread1 t = new Thread1(); t.start(); } }
1) class Thread1 extends Thread { public void run() {}}
2) class Thread1 implements Thread { public void run() {}}
3) class Thread1 implements Runnable { public void run() {}}
4) class Thread1 extends Runnable { public static void run() {}}
Explanation / Answer
public class Test12 {
public static void main(String[] args) {
Thread1 t = new Thread1();
t.start();
}
}
Answer: 1) class Thread1 extends Thread { public void run() {}}
Explanation:
To make any class thread:
a. you need to extends "Thread" class or
b. you need to implements "Runnable" interface
But to start a thread, you need "Thread" class object, because "start()" method inside "Thread" class
So, here we are creating Thread1 Object and calling start() method directly on Thread1 Object,
so Thread1 must extends Thread class
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.