+ 7
A wrong question in sololearn challenge (Java)?
I got the next question in Java challenge: Which of these methods can be used to make the main thread be executed last among all the threads? ( ) call() (*) sleep() ( ) join() ( ) stop() Sololearn says that correct answer is sleep(). But the correct answer is join(): The time of executing of other threads is unknown and can't be predicted, so you don't know how long main thread should be suspended inside sleep(). But when you execute the join() method of other thread, main thread will be suspended till other thread end. What do you think?
9 Antworten
+ 6
@~ swim ~
As it is discuss forum, I want to see other opinions. I can be wrong.
+ 5
For example, I acn write the next code:
public class Program
{
public static void main(String[] args) throws InterruptedException {
Thread[] threads = new Thread[10];
for(int i = 0; i < threads.length; ++i){
threads[i] = new Thread(){
public void run()
{
try{
//let's do some work,
//which take random time
sleep((long)(Math.random() * 500));
}catch(InterruptedException e){}
System.out.println(" other thread end");
}
};
threads[i].start();
}
Thread.sleep(250);
//uncomment next line to wait for all other threads
//for(Thread t: threads) t.join();
System.out.println("Main thread end");
}
}
The main thread will be last only if line
for(Thread t: threads) t.join();
is uncommented
https://code.sololearn.com/cNzdF0t9Rce5
+ 5
@Avinesh
But even if the question assumes method should be used with main thread,
sleep() is not correct answer because time of other thread execution may be very long (for example, waiting for input from a user, or waiting for some resource to be free. Or scheduler can give less time to other thread to be executed).
There is no guarantee that all other threads end their work before main thread will be resumed after being suspended inside sleep() method.
Avinesh,
thank you for your opinion. I see your point. I understood now what the author of the question wanted to ask: which method we can use to delay execution of the main thread for a while. At least this is the most appropriate question.
+ 3
@Avinesh
There is no any mention in the question that methods should be used only with main thread.
It is possible to call join() on main thread from a main thread like this:
Thread.currentThread().join();
but this is a deadlock!
Regarding your code:
the effect of the next lines:
Thread t = Thread.currentThread();
// ....
t.sleep(100);
is the same to:
Thread.sleep(100);
because sleep() is a static method of Thread,
so it doesn't need an object to call. It just suspends the caller for given time.
+ 1
I agree with your question and your explanation but are we not suppose to call these methods on the main thread?
This is just my part of contribution. Let me know if I'm wrong.
https://code.sololearn.com/c4z7zoYKpcpb/?ref=app
+ 1
But what I mentioned could be a possibility just like yours is one. The question might get lengthy if all the details were discussed.
But anyways if there are alternate possibilities then we cannot come to a conclusion.
+ 1
andriy kan that was some nice explanation there. I'm still learning the language and need some time to gel with few of the concepts.
Thanks for acknowledging my opinion and yes, what you just mentioned in your last paragraph should be the best way to frame that question so that sleep() turns out to be the right answer.