java多线程学习-一

开启多线程

    /**
     * @param args
     */
    public static void main(String[] args) {
        MyThread mt = new MyThread();       //4,创建Thread类的子类对象
        mt.start();                         //5,开启线程

        for(int i = 0; i < 1000; i++) {
            System.out.println("main()");
        }
    }

}

class MyThread extends Thread {             //1,继承Thread
    public void run() {                     //2,重写run方法
        for(int i = 0; i < 1000; i++) {     //3,将要执行的代码写在run方法中
            System.out.println("aaa");
        }
    }
public static void main(String[] args) {
        MyThread myThread = new MyThread();
        myThread.start();

        for (int i = 0; i <10000; i++) {
            System.out.println("mian()");
        }

    }

}
class MyThread extends Thread{
    public void run(){
        for (int i = 0; i < 10000; i++) {
            System.out.println("run()"+i);
        }
    }

猜你喜欢

转载自blog.51cto.com/357712148/2156198