java多线程的两种方式

package com.zmc.test;

public class test {
    public static void main(String[] args) {
        /**
         * demo1
         */
        Thread thread=new Thread(){
            public void run(){
                while (true) {
                    try {
                        //设置现成休眠500毫秒(0.5秒)
                        Thread.sleep(500);
                    } catch (InterruptedException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                    System.out.println("1:"+Thread.currentThread().getName());
                }
            }
        };          
        thread.start();
        
        /**
         * demo2
         */
        Thread thread2=new Thread(new Runnable() {
            @Override
            public void run() {
                while (true) {
                    System.out.println("2:"+Thread.currentThread().getName());
                }
            }
        });
        thread2.start();
    }

}

标签: none

添加新评论