So I have a program that prints Hello and Goodbye by using 2 threads. I need to print Hello 100 times and Goodbye 100 times. This is working fine, however, after it prints, the program hangs and doesn't finish. I have a feeling it is because the threads aren't finishing up.
I was wondering what the best way to do this is?
So my main is like this
public static void main(String [] args)
{
Object lock = new Object();
new HelloThread(lock).start();
new GoodbyeThread(lock).start();
}
And here is the HelloThread class. GoodbyeThread looks the same:
class HelloThread extends Thread
{
private Object lock;
private boolean isRunning = false;
HelloThread(Object l)
{
this.lock = l;
}
public void run()
{
for(int i = 0; i < 100; i++)
{
synchronized(lock)
{
isRunning = true;
System.out.println("Hello");
lock.notify();
try
{
lock.wait();
}
catch(InterruptedException e)
{
e.printStackTrace();
}
}
}
}
}
I was thinking there might be a way to have the isRunning variable be used to close the thread, however I'm not sure what the best way to go about it. Thanks for any help