Java 多线程实现

第一种方式

package demo3;

public class Threddemo {
    public static void main(String[] args) {
        MyThred mt = new MyThred();
        mt.start();
        for (int i = 0; i < 20; i++) {
            System.out.println("main"+i);
        }

    }
}


public class MyThred extends Thread {
    @Override
    public void run() {
        for (int i = 0; i < 20; i++) {
            System.out.println("run"+i);
        }
    }
}

第二种方式

package demo4;

public class runableimpl implements Runnable {
    @Override
    public void run() {
        for (int i = 0; i < 20; i++) {
            System.out.println(Thread.currentThread().getName() + i);
        }
    }
}



public class demo01runable {
    public static void main(String[] args) {
//        runableimpl run = new runableimpl();
//        Thread th = new Thread(run);
//        th.start();

//        Thread t = new Thread(new runableimpl());
//        t.start();

//        new Thread(){
//            @Override
//            public void run() {
//                for (int i = 0; i < 20; i++) {
//                    System.out.println(Thread.currentThread().getName()+i);
//                }
//            }
//        }.start();



        new Thread(new Runnable(){

            @Override
            public void run() {
                for (int i = 0; i < 20; i++) {
                    System.out.println(Thread.currentThread().getName()+i);
                }
            }
        }).start();

        for (int i = 0; i < 20; i++) {
            System.out.println(Thread.currentThread().getName()+i);
        }

    }
}

 

猜你喜欢

转载自www.cnblogs.com/Erick-L/p/10372766.html
0条评论
添加一条新回复