写了一个多线程程序,按序列打印奇数和偶数,直到序列达到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);
}
}
当程序运行时,它出来就好了,但它不会进入下一行,所以我可以为我想做的任何事情输入另一个命令。任何想法为什么这是我可以做些什么来解决它?
修改后的代码: 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);
}
}
答案 0 :(得分:1)
你没有停止你的线程池,所以它不会让你的主程序结束。
请考虑以下代码:
import java.util.concurrent.Executors;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.atomic.AtomicInteger;
class Worker implements Runnable {
private AtomicInteger i;
private Object lock;
private int max;
private int mod;
Worker(AtomicInteger i_, Object lock_, int max_, int mod_){
this.i = i_;
this.lock = lock_;
this.max = max_;
this.mod = mod_;
}
@Override
public void run(){
while (i.get() < max)
{
if(i.get() % 2 == mod)
{
System.out.print(" " + i.getAndAdd(1));
synchronized(lock){ lock.notify(); }
}
else
{
synchronized(lock)
{
try { lock.wait(); }
catch (InterruptedException e) { e.printStackTrace(); }
}
}
}
}
}
public class NumPrintTX
{
public static void main(String[] args)
{
final int max = 31;
final AtomicInteger i = new AtomicInteger(0);
ExecutorService dd = Executors.newFixedThreadPool(2);
final Object lock = new Object();
dd.execute(new Worker(i, lock, max, 0));
dd.execute(new Worker(i, lock, max, 1));
dd.shutdown();
}
}
如果没有dd.shutdown()
,您的程序将会挂起,尽管已经结束了。
主要的do-while循环和System.exit(0)
将起作用,但似乎有点“暴力”。
此外,此代码简化了您的线程代码,使其更具可读性,而无需更改基础逻辑。
答案 1 :(得分:0)
您需要通过调用System.exit(0);
来退出程序,其中0
是“正常”退出的退出状态代码。应该在你的循环打印完数字之后进行调用。
答案 2 :(得分:-1)
您需要明确关闭您的计划。所以,像这样:
public void close() {
// Put any closing code here, like joining Threads or whatever
System.exit(0); // 0 means it is a normal exit, i.e. there are no errors
}