Signup/Sign In
Ask Question
Not satisfied by the Answer? Still looking for a better solution?

Multithreading _ join() method

public class Main implements Runnable
{
public void run()
{
System.out.println("r1 ");
System.out.println("r2 ");
}

public static void main( String[] args )
{
Thread t = new Thread(new Main());
t.start();
System.out.println("m1 ");
t.join();
System.out.println("m2 ");
}
}
why this error?
Main.java:15: error: unreported exception InterruptedException; must be caught or declared to be thrown
t.join();
^
1 error
by

2 Answers

Jeremiahqwdit
# 010101;
seetharamrao31ewl
Join() method in thread class is used while implementing multi-threading. The join method will allow one thread to wait until another thread completes its execution.
As you are using single thread, so there is no need to use the join method.
Eliminate the join method so that you will not get the error and the output for your program after removing the join method is:
m1
m2
r1
r2

Login / Signup to Answer the Question.