21
2017
09

java中创建线程的两种方式

package com.brendan.cn.concurrent;

public class TestThread {

    public static void main(String[] args) {

        //第一种方式
        Thread thread = new Thread(){
            @Override
            public void run() {
                while(true){
                    try {
                        Thread.sleep(500);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    System.out.println("1:" + Thread.currentThread().getName());
                }
            }
        };
        thread.start();


        //第二种方式
        //一般选第二种方式
        Thread thread2 = new Thread(new Runnable(){
            @Override
            public void run() {
                while(true){
                    try {
                        Thread.sleep(500);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                    System.out.println("1:" + Thread.currentThread().getName());

                }

            }
        });
        thread2.start();








    }

}
上一篇:编程范式 18 冰淇淋商店 下一篇:bzoj3698