欧美一区二区三区老妇人-欧美做爰猛烈大尺度电-99久久夜色精品国产亚洲a-亚洲福利视频一区二区

java多線程實(shí)用代碼 java多線程實(shí)用代碼編譯

java 如何實(shí)現(xiàn)多線程

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()方法,即主線程。

公司主營(yíng)業(yè)務(wù):成都做網(wǎng)站、成都網(wǎng)站建設(shè)、移動(dòng)網(wǎng)站開(kāi)發(fā)等業(yè)務(wù)。幫助企業(yè)客戶(hù)真正實(shí)現(xiàn)互聯(lián)網(wǎng)宣傳,提高企業(yè)的競(jìng)爭(zhēng)能力。創(chuàng)新互聯(lián)是一支青春激揚(yáng)、勤奮敬業(yè)、活力青春激揚(yáng)、勤奮敬業(yè)、活力澎湃、和諧高效的團(tuán)隊(duì)。公司秉承以“開(kāi)放、自由、嚴(yán)謹(jǐn)、自律”為核心的企業(yè)文化,感謝他們對(duì)我們的高要求,感謝他們從不同領(lǐng)域給我們帶來(lái)的挑戰(zhàn),讓我們激情的團(tuán)隊(duì)有機(jī)會(huì)用頭腦與智慧不斷的給客戶(hù)帶來(lái)驚喜。創(chuàng)新互聯(lián)推出札達(dá)免費(fèi)做網(wǎng)站回饋大家。

如何用Java編寫(xiě)多線程

在java中要想實(shí)現(xiàn)多線程,有兩種手段,一種是繼續(xù)Thread類(lèi),另外一種是實(shí)現(xiàn)Runable接口。

對(duì)于直接繼承Thread的類(lèi)來(lái)說(shuō),代碼大致框架是:

?

123456789101112 class 類(lèi)名 extends Thread{ 方法1; 方法2; … public void run(){ // other code… } 屬性1; 屬性2; … }

先看一個(gè)簡(jiǎn)單的例子:

?

12345678910111213141516171819202122232425262728 /** * @author Rollen-Holt 繼承Thread類(lèi),直接調(diào)用run方法 * */class hello extends Thread { public hello() { } public hello(String name) { this.name = name; } public void run() { for (int i = 0; i 5; i++) { System.out.println(name + "運(yùn)行 " + i); } } public static void main(String[] args) { hello h1=new hello("A"); hello h2=new hello("B"); h1.run(); h2.run(); } private String name; }

【運(yùn)行結(jié)果】:

A運(yùn)行 0

A運(yùn)行 1

A運(yùn)行 2

A運(yùn)行 3

A運(yùn)行 4

B運(yùn)行 0

B運(yùn)行 1

B運(yùn)行 2

B運(yùn)行 3

B運(yùn)行 4

我們會(huì)發(fā)現(xiàn)這些都是順序執(zhí)行的,說(shuō)明我們的調(diào)用方法不對(duì),應(yīng)該調(diào)用的是start()方法。

當(dāng)我們把上面的主函數(shù)修改為如下所示的時(shí)候:

?

123456 public static void main(String[] args) { hello h1=new hello("A"); hello h2=new hello("B"); h1.start(); h2.start(); }

然后運(yùn)行程序,輸出的可能的結(jié)果如下:

A運(yùn)行 0

B運(yùn)行 0

B運(yùn)行 1

B運(yùn)行 2

B運(yùn)行 3

B運(yùn)行 4

A運(yùn)行 1

A運(yùn)行 2

A運(yùn)行 3

A運(yùn)行 4

因?yàn)樾枰玫紺PU的資源,所以每次的運(yùn)行結(jié)果基本是都不一樣的,呵呵。

注意:雖然我們?cè)谶@里調(diào)用的是start()方法,但是實(shí)際上調(diào)用的還是run()方法的主體。

那么:為什么我們不能直接調(diào)用run()方法呢?

我的理解是:線程的運(yùn)行需要本地操作系統(tǒng)的支持。

如果你查看start的源代碼的時(shí)候,會(huì)發(fā)現(xiàn):

?

1234567891011121314151617 public synchronized void start() { /** * This method is not invoked for the main method thread or "system" * group threads created/set up by the VM. Any new functionality added * to this method in the future may have to also be added to the VM. * * A zero status value corresponds to state "NEW". */ if (threadStatus != 0 || this != me) throw new IllegalThreadStateException(); group.add(this); start0(); if (stopBeforeStart) { stop0(throwableFromStop); } } private native void start0();

注意我用紅色加粗的那一條語(yǔ)句,說(shuō)明此處調(diào)用的是start0()。并且這個(gè)這個(gè)方法用了native關(guān)鍵字,次關(guān)鍵字表示調(diào)用本地操作系統(tǒng)的函數(shù)。因?yàn)槎嗑€程的實(shí)現(xiàn)需要本地操作系統(tǒng)的支持。

但是start方法重復(fù)調(diào)用的話,會(huì)出現(xiàn)java.lang.IllegalThreadStateException異常。

通過(guò)實(shí)現(xiàn)Runnable接口:

大致框架是:

?

123456789101112 class 類(lèi)名 implements Runnable{ 方法1; 方法2; … public void run(){ // other code… } 屬性1; 屬性2; … }

來(lái)先看一個(gè)小例子吧:

?

123456789101112131415161718192021222324252627282930 /** * @author Rollen-Holt 實(shí)現(xiàn)Runnable接口 * */class hello implements Runnable { public hello() { } public hello(String name) { this.name = name; } public void run() { for (int i = 0; i 5; i++) { System.out.println(name + "運(yùn)行 " + i); } } public static void main(String[] args) { hello h1=new hello("線程A"); Thread demo= new Thread(h1); hello h2=new hello("線程B"); Thread demo1=new Thread(h2); demo.start(); demo1.start(); } private String name; }

【可能的運(yùn)行結(jié)果】:

線程A運(yùn)行 0

線程B運(yùn)行 0

線程B運(yùn)行 1

線程B運(yùn)行 2

線程B運(yùn)行 3

線程B運(yùn)行 4

線程A運(yùn)行 1

線程A運(yùn)行 2

線程A運(yùn)行 3

線程A運(yùn)行 4

關(guān)于選擇繼承Thread還是實(shí)現(xiàn)Runnable接口?

其實(shí)Thread也是實(shí)現(xiàn)Runnable接口的:

?

12345678 class Thread implements Runnable { //… public void run() { if (target != null) { target.run(); } } }

其實(shí)Thread中的run方法調(diào)用的是Runnable接口的run方法。不知道大家發(fā)現(xiàn)沒(méi)有,Thread和Runnable都實(shí)現(xiàn)了run方法,這種操作模式其實(shí)就是代理模式。關(guān)于代理模式,我曾經(jīng)寫(xiě)過(guò)一個(gè)小例子呵呵,大家有興趣的話可以看一下:

Thread和Runnable的區(qū)別:

如果一個(gè)類(lèi)繼承Thread,則不適合資源共享。但是如果實(shí)現(xiàn)了Runable接口的話,則很容易的實(shí)現(xiàn)資源共享。

?

1234567891011121314151617181920212223 /** * @author Rollen-Holt 繼承Thread類(lèi),不能資源共享 * */class hello extends Thread { public void run() { for (int i = 0; i 7; i++) { if (count 0) { System.out.println("count= " + count--); } } } public static void main(String[] args) { hello h1 = new hello(); hello h2 = new hello(); hello h3 = new hello(); h1.start(); h2.start(); h3.start(); } private int count = 5; }

【運(yùn)行結(jié)果】:

count= 5

count= 4

count= 3

count= 2

count= 1

count= 5

count= 4

count= 3

count= 2

count= 1

count= 5

count= 4

count= 3

count= 2

count= 1

大家可以想象,如果這個(gè)是一個(gè)買(mǎi)票系統(tǒng)的話,如果count表示的是車(chē)票的數(shù)量的話,說(shuō)明并沒(méi)有實(shí)現(xiàn)資源的共享。

我們換為Runnable接口:

?

12345678910111213141516171819 /** * @author Rollen-Holt 繼承Thread類(lèi),不能資源共享 * */class hello implements Runnable { public void run() { for (int i = 0; i 7; i++) { if (count 0) { System.out.println("count= " + count--); } } } public static void main(String[] args) { hello he=new hello(); new Thread(he).start(); } private int count = 5; }

【運(yùn)行結(jié)果】:

count= 5

count= 4

count= 3

count= 2

count= 1

總結(jié)一下吧:

實(shí)現(xiàn)Runnable接口比繼承Thread類(lèi)所具有的優(yōu)勢(shì):

1):適合多個(gè)相同的程序代碼的線程去處理同一個(gè)資源

2):可以避免java中的單繼承的限制

3):增加程序的健壯性,代碼可以被多個(gè)線程共享,代碼和數(shù)據(jù)獨(dú)立。

所以,本人建議大家勁量實(shí)現(xiàn)接口。

?

java線程的經(jīng)典代碼

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();

}

}

JAVA程序設(shè)計(jì),多線程,求大神給一份可運(yùn)行的代碼

給你一個(gè)經(jīng)典的例子。run里面放空循環(huán)來(lái)觀察多線程是不合理的,空循環(huán)消耗時(shí)序極小,用sleep來(lái)間隔時(shí)間才是合理的。

class?RunnableDemo?implements?Runnable?{

private?Thread?t;

private?String?threadName;

RunnableDemo(?String?name)?{

threadName?=?name;

System.out.println("Creating?"?+??threadName?);

}

public?void?run()?{

System.out.println("Running?"?+??threadName?);

try?{

for(int?i?=?4;?i??0;?i--)?{

System.out.println("Thread:?"?+?threadName?+?",?"?+?i);

//?Let?the?thread?sleep?for?a?while.

Thread.sleep(50);

}

}catch?(InterruptedException?e)?{

System.out.println("Thread?"?+??threadName?+?"?interrupted.");

}

System.out.println("Thread?"?+??threadName?+?"?exiting.");

}

public?void?start?()?{

System.out.println("Starting?"?+??threadName?);

if?(t?==?null)?{

t?=?new?Thread?(this,?threadName);

t.start?();

}

}

}

public?class?TestThread?{

public?static?void?main(String?args[])?{

RunnableDemo?R1?=?new?RunnableDemo(?"Thread-1");

R1.start();

RunnableDemo?R2?=?new?RunnableDemo(?"Thread-2");

R2.start();

}???

}

網(wǎng)頁(yè)標(biāo)題:java多線程實(shí)用代碼 java多線程實(shí)用代碼編譯
網(wǎng)頁(yè)地址:http://chinadenli.net/article40/hpioeo.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供App設(shè)計(jì)做網(wǎng)站關(guān)鍵詞優(yōu)化移動(dòng)網(wǎng)站建設(shè)網(wǎng)站維護(hù)

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶(hù)投稿、用戶(hù)轉(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)

成都定制網(wǎng)站建設(shè)