线程1.1(演示如何通过Runnable来开发线程)

/**
 * 2018.8.30
 * 作者:小孟鱼
 * 功能:演示如何通过Runnable来开发线程
 *
 */
package com.xianchen;

public class Xianchen2 {
        public static void main(String[] args) {
            Dog dog=new Dog();
            
            //启动线程方法1
            //dog.run();
            
            //启动线程方法2
            Thread t=new Thread(dog);
            t.start();
        }
}

class Dog implements Runnable
{
    public void run()
    {
        int times=0;
        //while(true)是一个死循环
        while(true)
        {
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            
            times++;
            System.out.println("Hello world"+times);
            if(times==10)
            {
                break;
            }
        }
    }
}

猜你喜欢

转载自blog.csdn.net/weixin_42133768/article/details/82215878