你看清楚好吗?你写的while(true)是在线程池的外围啊,张老师写的是在线程里面,会一样吗?看看我按张老师的写法改的你的代码,再看不懂我就没话说了
- package demo2;
- import java.util.concurrent.Executors;
- public class Demo {
- public static void main(String[] args) {
-
- new test1().show1();
- new test2().show2();
- new test3().show3();
-
- }
- }
- class test1{
- public void show1(){
-
- Executors.newSingleThreadExecutor().execute(new Runnable() {
- public void run() {
- while (true) {
- System.out.println("1");
- }
- }
- });
-
- }
- }
- class test2{
- public void show2(){
-
- Executors.newSingleThreadExecutor().execute(new Runnable() {
- public void run() {
- while (true) {
- System.out.println("2");
- }
- }
- });
-
- }
- }
- class test3{
- public void show3(){
-
- Executors.newSingleThreadExecutor().execute(new Runnable() {
- public void run() {
- while (true) {
- System.out.println("3");
- }
- }
- });
-
- }
- }
-
复制代码 |