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

线程中途停止的问题
Java code

Thread t = new Thread(){
                public void run(){
                    Thread th1 = new Thread();
                    Thread th2 = new Thread();
                    Thread th3 = new Thread();
                    Thread th4 = new Thread();
                    
                    //这四个线程分别运行5秒
                    
                    try {
                        th1.start();
                        th1.join();
                        
                        th2.start();
                        th2.join();
                        
                        th3.start();
                        th3.join();
                        
                        th4.start();
                        th4.join();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            };
            t.start();


问一下:总线程t启动后,当运行th2过程中 怎样才可以把总线程t立即停止 我调用t.interrupt() 或者t.yield()语句后,它还是会继续执行th3线程和th4线程。
但是当th4线程运行时候 我去调用t.interrupt()语句 t线程就会停止运行。


------解决方案--------------------
可能是你th2实现的有问题,你可以把完整的代码贴出来看看。

th2.start();
th2.join();
当线程t block时,th2调t.interrupte(), 线程t会抛InterruptedException,
th3,th4应该没有机会执行.