第一種:用Thread類創(chuàng)建線程
創(chuàng)新互聯(lián)公司作為成都網(wǎng)站建設(shè)公司,專注網(wǎng)站建設(shè)、網(wǎng)站設(shè)計(jì),有關(guān)成都定制網(wǎng)站方案、改版、費(fèi)用等問題,行業(yè)涉及社區(qū)文化墻等多個(gè)領(lǐng)域,已為上千家企業(yè)服務(wù),得到了客戶的尊重與認(rèn)可。
public class ThreadDemo1
{
public static void main(String args[])
{
new TestThread().start();//調(diào)TestThread類的start函數(shù)(從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接口創(chuàng)建多線程
public class ThreadDemo2
{
public static void main(String args[])
{
TestThread tt = new TestThread();//創(chuàng)建TestThread類的一個(gè)實(shí)例
Thread t = new Thread(tt);//創(chuàng)建一個(gè)Thread類的實(shí)例
t.start();//使線程進(jìn)入Runnable狀態(tài)
while(true)
{
System.out.println("main thread is running");
}
}
}
class TestThread implements Tunnable
{
public void run()//線程的代碼段,當(dāng)執(zhí)行start()時(shí),線程從此處開始執(zhí)行
{
while(true)
{
System.out.println(Thread.currentThread().getName()"is running");
}
}
}
結(jié)論:第二種方法比較好。
文章標(biāo)題:Android多線程
轉(zhuǎn)載來于:http://muchs.cn/article6/pdjgog.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)頁設(shè)計(jì)公司、品牌網(wǎng)站制作、建站公司、網(wǎng)站設(shè)計(jì)、網(wǎng)站改版、網(wǎng)站設(shè)計(jì)公司
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)