日期:2014-05-20  浏览次数:20691 次

问个多线程Executors的小问题
我在做Thinking In Java第四版最后一章第三个练习

我写的是:
Java code

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class Ex3
{
    public static void main(String[] args)
    {        
        System.out.println("------Cached Pool------");
        ExecutorService execCached = Executors.newCachedThreadPool();
        execCached.execute(new Imex31());
        execCached.execute(new Imex32());
        execCached.execute(new Imex33());
        execCached.shutdown();
        
        System.out.println("------Fixed Pool------");
        ExecutorService execFixed = Executors.newFixedThreadPool(3);
        execFixed.execute(new Imex31());
        execFixed.execute(new Imex32());
        execFixed.execute(new Imex33());
        execFixed.shutdown();
        
        System.out.println("------Single Thread------");
        ExecutorService execSingle = Executors.newSingleThreadExecutor();
        execSingle.execute(new Imex31());
        execSingle.execute(new Imex32());
        execSingle.execute(new Imex33());
        execSingle.shutdown();
        
        /*Thread t1 = new Thread(new Imex31());
        Thread t2 = new Thread(new Imex32());
        Thread t3 = new Thread(new Imex33());
        
        t1.start();
        t2.start();
        t3.start();*/
    }

}

class Imex31 implements Runnable
{
    public Imex31()
    {
        System.out.println("Constructor 1 begin");
    }
    public void run()
    {
        for (int i = 1; i < 4; i++)
        {
            System.out.println("In the run ex1 " + i);
            Thread.yield();
        }
        System.out.println("ex1 complete");
        return;
    }
}

class Imex32 implements Runnable
{
    public Imex32()
    {
        System.out.println("Constructor 2 begin");
    }
    public void run()
    {
        for (int i = 1; i < 4; i++)
        {
            System.out.println("In the run ex2 " + i);
            Thread.yield();
        }
        System.out.println("ex2 complete");
        return;
    }
}

class Imex33 implements Runnable
{
    public Imex33()
    {
        System.out.println("Constructor 3 begin");
    }
    public void run()
    {
        for (int i = 1; i < 4; i++)
        {
            System.out.println("In the run ex3 " + i);
            Thread.yield();
        }
        System.out.println("ex3 complete");
        return;
    }
}



奇怪的是输出显示execFixed那几个线程没有shutdown就开始execSingle了,这是怎么回事呢?

谢谢先!!

------解决方案--------------------
你三个线程池之间并没有什么关系,三个线程池 每个分别处理三个不同的任务。

你的主线程是 main 方法 ,

ExecutorService execCached = Executors.newCachedThreadPool();
execCached.execute(new Imex31());
execCached.execute(new Imex32());
execCached.execute(new Imex33());
execCached.shutdown();

System.out.println("------Fixed Pool------");
ExecutorService execFixed = Executors.newFixedThreadPool(3);
execFixed.execute(new Imex31());
execFixed.execute(new Imex32());
execFixed.execute(new Imex33());
execFixed.shutdown();

System.out.println("------Single Thread------");
ExecutorService execSingle = Executors.newSingleThreadExecutor();
execSingle.execute(new Imex31());
execSingle.execute(new Imex32());
execSingle.execute(new Imex33());
execSingle.shutdown();

你在线程池上调用 shuntdown 是告诉线程池 ,在任务完成的时候结束线程而已。
再说 在线程池上面 shutdown 线程又不会阻塞, 那么后面的语句当然可以执行了啊。
你可以 在每个线程池开始后 也就是在 shutdown 后面加上 一句 TimeUnit.Second.sleep(5); 睡几秒过后等你线程池的任务任务执行完毕后在开始下一个线程池。


建议在把线程池 的API 仔细看看吧。

楼上说的家 synchronized 那是肯定都不对的。 因为根本不涉及到任何同步。