怎样用多线程快速编写一个小程序?

解决方案 »

  1.   

    public class TestThread extends Thread{ /**
     * @param args
     */
    public static void main(String[] args) {

    TestThread t = new TestThread();

    t.start();



    }

    public void run()
    {


    System.out.println("最小的多 线程 程序");
    }

    }
      

  2.   

    public class TestThread implements Runnable{ static int i = 0;
    @Override
    public void run() {
    while(i<100){
    System.out.println("thread="+i);
    i++;
    try {
    Thread.sleep(1000);
    } catch (InterruptedException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    }

    }

    public static void main(String[] args) {
    Thread tt = new Thread(new TestThread()); 
    Thread tt2 = new Thread(new TestThread()); 
    tt.start();
    tt2.start();
    }

    }
    tt跟tt2是两个线程
      

  3.   

    一楼和二楼 把多线程的两个方法 都讲了一遍
    实现Runnable接口比继承Thread要用的广泛 因为他更灵活 
    类只能继承一个父类 但可以实现多个接口 实现Runnable接口会让类更灵活丰富