Java多线程的创建(方法二)

来源:互联网 发布:美工和平面设计的区别 编辑:程序博客网 时间:2024/06/16 17:21
/* *  * 创建线程的第二种方式,实现Runnable接口 * 1.定义类实现Runnable接口 * 2.覆盖接口中的run方法,将线程的任务代码封装到run方法中 * 3.通过Thread类创建线程对象,并将Runnable接口的子类对象作为构造函数的参数进行传递 * 4.调用线程对象的start方法开启线程 *  * */class Demo1 implements Runnable{    public void run()    {        for(int x=0;x<10;x++)        {            System.out.println("....x="+x+Thread.currentThread().getName());        }    }}public class Demo  {    public static void main(String[] args)     {       Demo1 d=new Demo1();            Thread t1=new Thread(d);            Thread t2=new Thread(d);            t1.start();            t2.start();    }}

方法一链接 http://blog.csdn.net/qq_31634461/article/details/78295449