Wrote a multi-thread program to print out odd and even numbers in sequence untill the sequence reached 30.
import java.util.concurrent.Executor;
import java.util.concurrent.Executors;
import java.util.concurrent.atomic.AtomicInteger;
public class NumPrintTX
{
public static void main(String[] args)
{
final int max = 31;
final AtomicInteger i = new AtomicInteger(0);
Executor dd = Executors.newFixedThreadPool(2);
final Object lock = new Object();
dd.execute(new Runnable()
{
@Override
public void run()
{
while (i.get() < max)
{
if (i.get() % 2 == 0)
{
System.out.print(" " + i.getAndAdd(1));
synchronized(lock)
{
lock.notify();
}
}
else
{
synchronized(lock)
{
try
{
lock.wait();
}
catch (InterruptedException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
}
});
dd.execute(new Runnable()
{
@Override
public void run()
{
while (i.get() < max)
{
if (i.get() % 2 != 0)
{
System.out.print(" " + i.getAndAdd(1));
synchronized(lock)
{
lock.notify();
}
}
else
{
synchronized(lock)
{
try
{
lock.wait();
}
catch (InterruptedException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
}
});
do
{
try
{
Thread.currentThread().sleep(1000);
}
catch (InterruptedException e)
{
e.printStackTrace();
}
}
while (i.get() != max);
}
}
when the program runs, it comes out just fine, but it won’t go to the next line so i could enter another command for whatever i want to do next. Any ideas why this is and what i can do to fix it?
modified code:
import java.util.concurrent.Executor;
import java.util.concurrent.Executors;
import java.util.concurrent.atomic.AtomicInteger;
public class NumPrintTX
{
public static void main(String[] args)
{
final int max = 31;
final AtomicInteger i = new AtomicInteger(0);
Executor dd = Executors.newFixedThreadPool(2);
final Object lock = new Object();
dd.execute(new Runnable()
{
@Override
public void run()
{
while (i.get() < max)
{
if (i.get() % 2 == 0)
{
System.out.print(" " + i.getAndAdd(1));
synchronized(lock)
{
lock.notify();
}
}
else
{
synchronized(lock)
{
try
{
lock.wait();
}
catch (InterruptedException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
}
});
dd.execute(new Runnable()
{
@Override
public void run()
{
while (i.get() < max)
{
if (i.get() % 2 != 0)
{
System.out.print(" " + i.getAndAdd(1));
synchronized(lock)
{
lock.notify();
}
}
else
{
synchronized(lock)
{
try
{
lock.wait();
}
catch (InterruptedException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
}
});
do
{
try
{
Thread.currentThread().sleep(1000);
}
catch (InterruptedException e)
{
e.printStackTrace();
}
}
while (i.get() != max);
}
public void close()
{
System.exit(0);
}
}
You’re not stopping your thread pool, so it’s not letting your main program end.
Consider the following code:
Without the
dd.shutdown(), your program will hang despite getting to the end.The main do-while loop and
System.exit(0)will work but seems a bit “brute-force”.Also, this code streamlines your thread code and makes it much more readable without changing the underlying logic.