線程用到Thread或者Runnable接口(Thread也操作了Runnable接口)

創(chuàng)新互聯(lián)是一家專(zhuān)注于成都網(wǎng)站設(shè)計(jì)、網(wǎng)站制作與策劃設(shè)計(jì),當(dāng)陽(yáng)網(wǎng)站建設(shè)哪家好?創(chuàng)新互聯(lián)做網(wǎng)站,專(zhuān)注于網(wǎng)站建設(shè)十多年,網(wǎng)設(shè)計(jì)領(lǐng)域的專(zhuān)業(yè)建站公司;建站業(yè)務(wù)涵蓋:當(dāng)陽(yáng)等地區(qū)。當(dāng)陽(yáng)做網(wǎng)站價(jià)格咨詢:18982081108
繼承了Thread類(lèi)后需要重載其run方法,在方法里寫(xiě)你需要完成的事情,開(kāi)始線程是調(diào)用其start方法。
操作Runnable接口必須實(shí)現(xiàn)其run方法,在方法里寫(xiě)你需要完成的事情,Runnable接口沒(méi)有start方法,所以啟動(dòng)線程還是需要依靠Thread類(lèi) new Thread(Runnable runnable).start();
一般項(xiàng)目中多是操作接口,因?yàn)轭?lèi)只能單繼承,接口可以操作多個(gè)。
繼承Thread類(lèi)來(lái)實(shí)現(xiàn)多線程:
當(dāng)我們自定義的類(lèi)繼承Thread類(lèi)后,該類(lèi)就為一個(gè)線程類(lèi),該類(lèi)為一個(gè)獨(dú)立的執(zhí)行單元,線程代碼必須編寫(xiě)在run()方法中,run方法是由Thread類(lèi)定義,我們自己寫(xiě)的線程類(lèi)必須重寫(xiě)run方法。
run方法中定義的代碼為線程代碼,但run方法不能直接調(diào)用,如果直接調(diào)用并沒(méi)有開(kāi)啟新的線程而是將run方法交給調(diào)用的線程執(zhí)行
要開(kāi)啟新的線程需要調(diào)用Thread類(lèi)的start()方法,該方法自動(dòng)開(kāi)啟一個(gè)新的線程并自動(dòng)執(zhí)行run方法中的內(nèi)容
? ? ?
請(qǐng)點(diǎn)擊輸入圖片描述
結(jié)果: ? ? ? ? ? ?
? ? ?
請(qǐng)點(diǎn)擊輸入圖片描述
*java多線程的啟動(dòng)順序不一定是線程執(zhí)行的順序,各個(gè)線程之間是搶占CPU資源執(zhí)行的,所有有可能出現(xiàn)與啟動(dòng)順序不一致的情況。
CPU的調(diào)用策略:
如何使用CPU資源是由操作系統(tǒng)來(lái)決定的,但操作系統(tǒng)只能決定CPU的使用策略不能控制實(shí)際獲得CPU執(zhí)行權(quán)的程序。
線程執(zhí)行有兩種方式:
1.搶占式:
目前PC機(jī)中使用最多的一種方式,線程搶占CPU的執(zhí)行權(quán),當(dāng)一個(gè)線程搶到CPU的資源后并不是一直執(zhí)行到此線程執(zhí)行結(jié)束,而是執(zhí)行一個(gè)時(shí)間片后讓出CPU資源,此時(shí)同其他線程再次搶占CPU資源獲得執(zhí)行權(quán)。
2.輪循式;
每個(gè)線程執(zhí)行固定的時(shí)間片后讓出CPU資源,以此循環(huán)執(zhí)行每個(gè)線程執(zhí)行相同的時(shí)間片后讓出CPU資源交給下一個(gè)線程執(zhí)行。
java中多線程的實(shí)現(xiàn)方式有兩種,一種是繼承java.lang.Thread類(lèi),另一種是實(shí)現(xiàn)java.lang.Runnable接口。下面是兩種方式的簡(jiǎn)單代碼。繼承Thread類(lèi)方式:import java.lang.Thread; //用集成Thread類(lèi)方式實(shí)現(xiàn)多線程。 public class Test{ public static void main(String arg[]){ T t1=new T(); T t2=new T(); //更改新線程名稱(chēng) t1.setName("t1"); t2.setName("t2"); //啟動(dòng)線程 t1.start(); t2.start(); } } class T extends Thread{ //重寫(xiě)run()方法 public void run(){ System.out.println(this.getName()); } }輸出結(jié)果為:t1t2實(shí)現(xiàn)Runnable接口方式:在使用Runnable接口時(shí)需要建立一個(gè)Thread實(shí)例。因此,無(wú)論是通過(guò)Thread類(lèi)還是Runnable接口建立線程,都必須建立Thread類(lèi)或它的子類(lèi)的實(shí)例。import java.lang.*; //用實(shí)現(xiàn)Runnable接口的方式實(shí)現(xiàn)多線程。 public class Test{ public static void main(String arg[]){ T t1=new T(); T t2=new T(); //一定要實(shí)例化Thread對(duì)象,將實(shí)現(xiàn)Runnable接口的對(duì)象作為參數(shù)傳入。 Thread th1=new Thread(t1,"t1"); Thread th2=new Thread(t2,"t2"); //啟動(dòng)線程 th1.start(); th2.start(); } } class T implements Runnable{ //重寫(xiě)run()方法 public void run(){ System.out.println(Thread.currentThread().getName()); } }輸出結(jié)果為:t1t2public void run()方法是JAVA中線程的執(zhí)行體方法,所有線程的操作都是從run方法開(kāi)始,有點(diǎn)類(lèi)似于main()方法,即主線程。
package threadgroup;
class ThreadDemo3 extends Thread {
private String name;
private int delay;
public ThreadDemo3(String sname, int i_delay) {
name = sname;
delay = i_delay;
}
public void run() {
try {
sleep(delay);
} catch (InterruptedException e) {
}
System.out.println("多線程測(cè)試!\n" + name + "\n" + delay);
}
}
public class testMyThread {
public static void main(String[] args) {
ThreadDemo3 th1,th2,th3;
th1 = new ThreadDemo3("線程1", (int) (Math.random() * 900));
th2 = new ThreadDemo3("線程2", (int) (Math.random() * 900));
th3 = new ThreadDemo3("線程3", (int) (Math.random() * 900));
th1.start();
th2.start();
th3.start();
}
}
package threadgroup;
public class threadDemo {
public static void main(String[] args) {
Thread t = Thread.currentThread();
t.setName("你好嗎?");
System.out.println("正在進(jìn)行的Thread是:" + t);
try {
for (int i = 0; i 5; i++) {
System.out.println("我不叫穆繼超" + i);
Thread.sleep(3000);
}
} catch (Exception e) {
// TODO: handle exception
System.out.println("Thread has wrong" + e.getMessage());
}
}
}
package threadgroup;
public class threadDemo2 implements Runnable {
public threadDemo2() {
Thread t1 = Thread.currentThread();
t1.setName("第一個(gè)主進(jìn)程");
System.out.println("正在運(yùn)行" + t1);
Thread t2 = new Thread(this, "");
System.out.println("在創(chuàng)建一個(gè)進(jìn)程");
t2.start();
try {
System.out.println("使他進(jìn)入第一個(gè)睡眠狀態(tài)");
Thread.sleep(2000);
} catch (InterruptedException e) {
System.out.println("Thread has wrong" + e.getMessage());
}
System.out.println("退出第一個(gè)進(jìn)程");
}
public void run() {
try {
for (int i = 0; i 5; i++) {
System.out.println("進(jìn)程" + i);
Thread.sleep(3000);
}
} catch (InterruptedException e) {
// TODO: handle exception
System.out.println("Thread has wrong" + e.getMessage());
}
System.out.println("退出第二個(gè)進(jìn)程");
}
public static void main(String[] args) {
new threadDemo2();
}
}
class MyJoinThread extends Thread
{
public MyJoinThread()
{
super();
}
public MyJoinThread(String name)
{
super(name);
}
public void run()
{
for(int i=0;i10;i++)
{
System.out.println(super.getName()+":"+i);
}
}
}
public class JoinTest
{
public static void main(String[] args)
{
MyJoinThread thread=new MyJoinThread("第1個(gè)線程");
thread.start();
for(int i=0;i10;i++)
{
System.out.println(Thread.currentThread().getName()+":"+i);
if(i==5)
{
try
{
thread.join();
}
catch (InterruptedException e)
{
System.out.println("線程被中斷");
}
}
}
}
}
文章題目:java中線程實(shí)現(xiàn)代碼,java線程例子
當(dāng)前路徑:http://chinadenli.net/article15/dsegegi.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站營(yíng)銷(xiāo)、App開(kāi)發(fā)、網(wǎng)站建設(shè)、企業(yè)網(wǎng)站制作、定制網(wǎng)站、網(wǎng)站導(dǎo)航
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容