Android多线程

第一种:用Thread类创建线程

创新互联公司作为成都网站建设公司,专注网站建设、网站设计,有关成都定制网站方案、改版、费用等问题,行业涉及社区文化墙等多个领域,已为上千家企业服务,得到了客户的尊重与认可。

public class ThreadDemo1

{

   public static void main(String args[])

   {

       new TestThread().start();//调TestThread类的start函数(从Thread类继承而来的)

       while(true)

       {

                     System.out.println("main thread is running");

       }

   }

}

class TestThread extendsThread

{

    public void run()

    {      

       while(true)

      {

           System.out.println(Thread.currentThread().getName() "is running");

       }

    }
}

第二种:使用Runnable接口创建多线程

public class ThreadDemo2

{

   public static void main(String args[])

   {

       TestThread tt = new TestThread();//创建TestThread类的一个实例

       Thread t = new Thread(tt);//创建一个Thread类的实例

       t.start();//使线程进入Runnable状态

       while(true)

       {

            System.out.println("main thread is running");

       }

   }

}

class TestThread implements Tunnable

{

    public void run()//线程的代码段,当执行start()时,线程从此处开始执行

   {

        while(true)

        {

            System.out.println(Thread.currentThread().getName()"is running");

        }

   }

}

结论:第二种方法比较好。


当前名称:Android多线程
URL地址:http://scjbc.cn/article/pdjgog.html

其他资讯