先贴代码:import java.util.Scanner;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
public class testpool2 {
private ExecutorService pool = Executors.newFixedThreadPool(2);
public void start() {
Scanner in = new Scanner(System.in);
boolean exit = false;
while (!exit) {
String s = in.nextLine();
switch (Integer.valueOf(s) % 4) {
case 0:
pool.execute(new t1());
break;
case 1:
pool.execute(new t2());
break;
default:
exit = true;
break;
}
if (Integer.valueOf(s) % 3 == 1) ;
else ;
}
System.out.println("over");
}
public static void main(String args[]) {
testpool2 tp = new testpool2();
tp.start();
}}class t1 implements Runnable {
public void run() {
System.out.println(this.getClass().toString() + ";hascode:" + this.hashCode());
System.out.println("run over");
}
}
class t2 implements Runnable {
public void run() {
System.out.println(this.getClass().toString() + ";hascode:" + this.hashCode());
System.out.println("run over");
}
}

解决方案 »

  1.   

    运行结果如下:
    输入:
    0
    输出:
    class t1;hascode:23746042
    run over
    输入:
    1
    输出:
    class t2;hascode:5718203
    run over
    输入:
    0
    输出:
    class t1;hascode:5947506
    run over
    输入:
    1
    输出:
    class t2;hascode:1088076
    run over
    输入:
    2
    输出:
    over
      

  2.   

    疑问:
    1:线程池应该是指线程对象用完并不释放,而是返回到线程池,但是这里所有的hashcode都不一致,那么应该是说上次用的被释放了;
    2:hashcode是否可以唯一标识一个对象,如果不是那么我怎么判断;
    3:或者是我这里那里错误了
      

  3.   

    疑问: 
    1:线程池应该是指线程对象用完并不释放,而是返回到线程池,但是这里所有的hashcode都不一致,那么应该是说上次用的被释放了; 
    2:hashcode是否可以唯一标识一个对象,如果不是那么我怎么判断; 
    3:或者是我这里那里错误了
    4.对了还有个问题忘了问,为什么主线程都结束了,但是程序还没有退出;我试过直接建立线程的话,如果关闭是全部关闭的
    答:
    1)线程池应该是指线程对象用完并不释放,而是返回到线程池.当然是的.你的代码输出的不是线程池中的线程.你代码应该这样写:class t1 implements Runnable {
        public void run() {
            System.out.println("线程池中是哪个线程在运行我的run?名字:"+
             Thread.currentThread().getName()+" ID:"+
             Thread.currentThread().getId());
            
        }
    }你再看看输出,你就明白了.2),3):参见1)中回答.
    4)当线程池启动后,它当中的线程就一直处于运行状态(或随时接收新任务的运行状态),因而:只好还有一个非守护线程在运行,程序都是结束不了的(那些线程池中的线程还在运行呐). 
      

  4.   

    不太明白线程到底是指什么,我修改代码如下:
    import java.util.Scanner;
    import java.util.concurrent.ExecutorService;
    import java.util.concurrent.Executors;
    public class testpool2 {
    private ExecutorService pool = Executors.newFixedThreadPool(2);
    public void start() {
    Scanner in = new Scanner(System.in);
    boolean exit = false;
    for (int i = 0; i < 2; i++) pool.execute(new t1());
    while (!exit) {
    String s = in.nextLine();
    switch (Integer.valueOf(s) % 4) {
    case 0:
    pool.execute(new t1());
    break;
    case 1:
    pool.execute(new t2());
    break;
    default:
    exit = true;
    break;
    }
    }
    System.out.println("over");
    }
    public static void main(String args[]) {
    testpool2 tp = new testpool2();
    tp.start();
    }}class t1 implements Runnable {
    public void run() {
    System.out.println(this.getClass().toString() + ";hascode:" + this.hashCode() + ";name:" + Thread.currentThread().getName() + ";ID:" + Thread.currentThread().getId());
    }
    }
    class t2 implements Runnable {
    public void run() {
    System.out.println(this.getClass().toString() + ";hascode:" + this.hashCode() + ";name:" + Thread.currentThread().getName() + ";ID:" + Thread.currentThread().getId());
    }
    }结果如下:
    输出:
    class t1;hascode:23746042;name:pool-1-thread-1;ID:8
    class t1;hascode:15244180;name:pool-1-thread-2;ID:9
    输入:
    0
    输出:
    class t1;hascode:5718203;name:pool-1-thread-1;ID:8
    输入:
    1
    输出:
    class t2;hascode:1088076;name:pool-1-thread-2;ID:9这里可以看出来id为9的线程开始是class t1;第二次为class t2;他们连类型都不一样了,怎么会是没有释放呢;或者我对线程的理解有错误
      

  5.   

    线程没有被释放!而是线程执行的方法(RUN)被你替换而已!,在Runnable输出Thread.currentThread().getName(),你会发现线程就只有两个,在变化的是Runnable对象,之所以你的Runbable的hashcode在变化,是因为你的Runnable对象在变化,你每次都是新的对象,不同对象,如果不重写hashcode方法,hashcode基本是不一样的,当然也有可能一样,这根你设计的算法有关,学完数据结构就知道了,如果你的Runnable加上一个死循环,执行两次execute后再执行就不行了,因为你的池就是2的大小。而且池中的两个线程都处于运行状态。真正想判断两个对象是否相等,重写equals方法,自己定义相当的准则就OK