Advice on a homework quesiton
Hellow again folks. I have a homework question that asks me to implement two classes that sets two concurrent threads running, one implementing Runnable and the other extending Thread. This part is ok, and it does work (no need to point out the dangers of doing this type of implementation in the real world, that's already implied). Anyway, here is the code I've written;
The main method;
Code:
package inout;
public class Main {
public static void main(String[] args)
{
In in = new In();
Thread t1 = new Thread(in);
Out out = new Out("Out the door");
t1.start();
out.start();
}
}
The In class:
Code:
package inout;
public class In implements Runnable
{
private static final int PRINT_IN = 5;
private String in = "In *";
public void run()
{
for (int i=0; i< PRINT_IN; i++)
{
System.out.print(in);
}
try
{
Thread.sleep(20);
}
catch(InterruptedException e)
{
System.out.println(e.getMessage());
}
}
}
The out class:
Code:
package inout;
public class Out extends Thread
{
private static final int PRINT_OUT = 4;
private String out = "Out *";
public Out(String thisThread)
{
super();
Thread t2 = new Thread();
t2.setName(thisThread);
}
public void run()
{
for (int i = 0; i<PRINT_OUT; i++)
{
System.out.print(out);
}
try
{
Thread.sleep(20);
}
catch (InterruptedException e)
{
System.out.println(e.getMessage());
}
}
}
The advice I'm looking for is this. The next part of the question askes me to instruct main to wait until both the threads have executed, then print out a line. The only way I can think of doing this within the main class is to create an inner class that also implements Runnable with a method that will wait while t1.isAlive() && out.isAlive() to be false. Is this the correct implementation in your opinion or am I missing something simple? There doesn't seem to be enough of a points allocation to merit this amount of effort. It also advises to look at the Thread API for a suitable method to implement (while() is inhereted from Object at this point). I can't implement while() directly as this will cause a "non static method while() cannot be referenced from a static context" error.
Thanks in advance
Re: Advice on a homework quesiton
Look at the Thread class's join() method.
Re: Advice on a homework quesiton
Quote:
Originally Posted by
Norm
Look at the Thread class's join() method.
Hi again Norm
Yea, I was looking down that road but not too sure how to implement it. I ended up with this:
Code:
try
{
Thread.sleep(1000);
}
catch (Exception e)
{
System.out.println(e.getMessage());
}
System.out.println();
System.out.println("FINISHED");
}
In the main method but this doesn't seem right to me. How do I know that 1000ms is enough for the threads to finish their jobs? I may well be over analysing this but I think your right. Will keep plugging away at it when time isn't so critical.
Thanks again for the advice. Do you have any examples of join() implementations that I can look at?
Cheers
Re: Advice on a homework quesiton
No I don't have any examples. Probably some on the forum, try Searching.
Re: Advice on a homework quesiton