先贴代码: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");
}
}
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");
}
}
输入:
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
1:线程池应该是指线程对象用完并不释放,而是返回到线程池,但是这里所有的hashcode都不一致,那么应该是说上次用的被释放了;
2:hashcode是否可以唯一标识一个对象,如果不是那么我怎么判断;
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)当线程池启动后,它当中的线程就一直处于运行状态(或随时接收新任务的运行状态),因而:只好还有一个非守护线程在运行,程序都是结束不了的(那些线程池中的线程还在运行呐).
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;他们连类型都不一样了,怎么会是没有释放呢;或者我对线程的理解有错误