What is the most likely result?

Given:

public class SleepOtherThread {

public static void main(String[] args) throws InterruptedException {

Runnable r = new Runnable() {

public void run() {

System.out.print(Thread.currentThread().getName());

}

};

Thread t1 = new Thread(r, “One “);

t1.start();

t1.sleep(2000);

Thread t2 = new Thread(r, “Two “);

t2.start();

t2.sleep(1000);

System.out.print(“Main “);

}

}

What is the most likely result?

Given:

public class SleepOtherThread {

public static void main(String[] args) throws InterruptedException {

Runnable r = new Runnable() {

public void run() {

System.out.print(Thread.currentThread().getName());

}

};

Thread t1 = new Thread(r, “One “);

t1.start();

t1.sleep(2000);

Thread t2 = new Thread(r, “Two “);

t2.start();

t2.sleep(1000);

System.out.print(“Main “);

}

}

What is the most likely result?

A.
Main One Two

B.
Main Two One

C.
One Two Main

D.
One Main Two

E.
Two Main One



Leave a Reply 5

Your email address will not be published. Required fields are marked *


Charlie

Charlie

sleep() is a static method of Thread class to sleep the current executing thread, so t1.sleep() is identical to Thread.sleep(), to sleep the main thread.

Luiz

Luiz

Thanks for the explanation.

C is the correct answer.

DonDorian

DonDorian

+1 Tested