public class C {    public static void main(String[] args) {        Thread t = new Thread() {            @Override            public void run() {                // TODO Auto-generated method stub                for (int i = 0; i < 100; i++) {                    System.out.println("线程" + Thread.currentThread().getName()                            + ":" + i);                    if (i == 20) {                        // break;                        return;                    }                }            }         };        t.start();    }}public class C {     public static void main(String[] args) {        Thread t = new Thread() {             private boolean finished;             @Override            public void run() {                // TODO Auto-generated method stub                while (true) {                    doSomeWork();                    if (finished) {                        System.out.println("线程终止");                        System.err.println("sssssssssss");                        break;                    }                }            }             private void doSomeWork() {                System.out.println("doSomeWork");                finished = true;            }        };        t.start();    }}public class C {    public static void main(String[] args) {        Thread t = new Thread() {            @Override            public void run() {                // TODO Auto-generated method stub                for (int i = 0; i < 100; i++) {                     try{                         Thread.sleep(1000);                         if(i==5)                             Thread.currentThread().interrupt();                     }catch(InterruptedException e){                         System.out.println("异常抛出!");                         throw new RuntimeException();                     }                     System.out.println("线程:"+i);                }            }         };        t.start();    }}原文:http://www.cnblogs.com/dolphin007/p/4416042.html