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

请问为什么没有效果
Java code

package com.java;

public class Myjava21 {

    /**
     * 多线程  :多个实例同时交替运行
     * 1,实现多线程有2种方式
     *    a,继承 Thread类(类本身也实现了Runnable接口),覆写 run()方法,然后再用 start()方法调用。
     *       为什么要用start()方法来调用呢?因为在执行run()前需要做一系列的准备工作,例如检查
     *       系统是否支持多线程操作,如果没有抛出异常,则调用run()方法。
     *    b,实现Runnable接口
     *       实现后,必须重写run()方法,但是在Runnable接口中,并无start()方法。所以还必须借助
     *       Thread类来实现多线程,因为Thread的构造函数为 public Thread(Runnable target)
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
      System.out.println("-----用继承Thread来实现多线程---------");
      Mythread21 my=new Mythread21("线程A");
      Mythread21 my1=new Mythread21("线程B");
      my.start();
      my1.start();
      System.out.println("------用实现Runnable接口来实现多线程---------");
      
      Myth21 m1=new Myth21("线程C");
      Myth21 m2=new Myth21("线程D");
      Thread t=new Thread(m1);
      Thread t1=new Thread(m2);
      t.start();
      t1.start();
      
    }
}

class Mythread21 extends Thread  //用继承的Thread类来实现多线程
{   
    private String myming;

    public Mythread21(String myming)
    {
        this.myming=myming;
    }

    public void run()
    {
        for(int i=0;i<200;i++)
        {   
            System.out.println(this.myming+"正在运行第:"+i+"步");
        }
    }
}

class Myth21 implements Runnable //实现接口
{  
    private String myming;

    public Myth21(String myming)
    {
        this.myming=myming;
    }
    public void run()
    {
        for(int i=0;i<200;i++)
        {   
            System.out.println(this.myming+"正在运行第:"+i+"步");
        }
    }
}




------解决方案--------------------
the start() method is:
Causes this thread to begin execution; the Java Virtual Machine calls the run method of this thread.