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

java多線程實用代碼 java多線程實用代碼編譯

java 如何實現多線程

java中多線程的實現方式有兩種,一種是繼承java.lang.Thread類,另一種是實現java.lang.Runnable接口。下面是兩種方式的簡單代碼。繼承Thread類方式:import java.lang.Thread; //用集成Thread類方式實現多線程。 public class Test{ public static void main(String arg[]){ T t1=new T(); T t2=new T(); //更改新線程名稱 t1.setName("t1"); t2.setName("t2"); //啟動線程 t1.start(); t2.start(); } } class T extends Thread{ //重寫run()方法 public void run(){ System.out.println(this.getName()); } }輸出結果為:t1t2實現Runnable接口方式:在使用Runnable接口時需要建立一個Thread實例。因此,無論是通過Thread類還是Runnable接口建立線程,都必須建立Thread類或它的子類的實例。import java.lang.*; //用實現Runnable接口的方式實現多線程。 public class Test{ public static void main(String arg[]){ T t1=new T(); T t2=new T(); //一定要實例化Thread對象,將實現Runnable接口的對象作為參數傳入。 Thread th1=new Thread(t1,"t1"); Thread th2=new Thread(t2,"t2"); //啟動線程 th1.start(); th2.start(); } } class T implements Runnable{ //重寫run()方法 public void run(){ System.out.println(Thread.currentThread().getName()); } }輸出結果為:t1t2public void run()方法是JAVA中線程的執(zhí)行體方法,所有線程的操作都是從run方法開始,有點類似于main()方法,即主線程。

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

如何用Java編寫多線程

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

對于直接繼承Thread的類來說,代碼大致框架是:

?

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

先看一個簡單的例子:

?

12345678910111213141516171819202122232425262728 /** * @author Rollen-Holt 繼承Thread類,直接調用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 + "運行 " + i); } } public static void main(String[] args) { hello h1=new hello("A"); hello h2=new hello("B"); h1.run(); h2.run(); } private String name; }

【運行結果】:

A運行 0

A運行 1

A運行 2

A運行 3

A運行 4

B運行 0

B運行 1

B運行 2

B運行 3

B運行 4

我們會發(fā)現這些都是順序執(zhí)行的,說明我們的調用方法不對,應該調用的是start()方法。

當我們把上面的主函數修改為如下所示的時候:

?

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

然后運行程序,輸出的可能的結果如下:

A運行 0

B運行 0

B運行 1

B運行 2

B運行 3

B運行 4

A運行 1

A運行 2

A運行 3

A運行 4

因為需要用到CPU的資源,所以每次的運行結果基本是都不一樣的,呵呵。

注意:雖然我們在這里調用的是start()方法,但是實際上調用的還是run()方法的主體。

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

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

如果你查看start的源代碼的時候,會發(fā)現:

?

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

注意我用紅色加粗的那一條語句,說明此處調用的是start0()。并且這個這個方法用了native關鍵字,次關鍵字表示調用本地操作系統(tǒng)的函數。因為多線程的實現需要本地操作系統(tǒng)的支持。

但是start方法重復調用的話,會出現java.lang.IllegalThreadStateException異常。

通過實現Runnable接口:

大致框架是:

?

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

來先看一個小例子吧:

?

123456789101112131415161718192021222324252627282930 /** * @author Rollen-Holt 實現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 + "運行 " + 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; }

【可能的運行結果】:

線程A運行 0

線程B運行 0

線程B運行 1

線程B運行 2

線程B運行 3

線程B運行 4

線程A運行 1

線程A運行 2

線程A運行 3

線程A運行 4

關于選擇繼承Thread還是實現Runnable接口?

其實Thread也是實現Runnable接口的:

?

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

其實Thread中的run方法調用的是Runnable接口的run方法。不知道大家發(fā)現沒有,Thread和Runnable都實現了run方法,這種操作模式其實就是代理模式。關于代理模式,我曾經寫過一個小例子呵呵,大家有興趣的話可以看一下:

Thread和Runnable的區(qū)別:

如果一個類繼承Thread,則不適合資源共享。但是如果實現了Runable接口的話,則很容易的實現資源共享。

?

1234567891011121314151617181920212223 /** * @author Rollen-Holt 繼承Thread類,不能資源共享 * */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; }

【運行結果】:

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

大家可以想象,如果這個是一個買票系統(tǒng)的話,如果count表示的是車票的數量的話,說明并沒有實現資源的共享。

我們換為Runnable接口:

?

12345678910111213141516171819 /** * @author Rollen-Holt 繼承Thread類,不能資源共享 * */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; }

【運行結果】:

count= 5

count= 4

count= 3

count= 2

count= 1

總結一下吧:

實現Runnable接口比繼承Thread類所具有的優(yōu)勢:

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

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

3):增加程序的健壯性,代碼可以被多個線程共享,代碼和數據獨立。

所以,本人建議大家勁量實現接口。

?

java線程的經典代碼

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("多線程測試!\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("正在進行的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("第一個主進程");

System.out.println("正在運行" + t1);

Thread t2 = new Thread(this, "");

System.out.println("在創(chuàng)建一個進程");

t2.start();

try {

System.out.println("使他進入第一個睡眠狀態(tài)");

Thread.sleep(2000);

} catch (InterruptedException e) {

System.out.println("Thread has wrong" + e.getMessage());

}

System.out.println("退出第一個進程");

}

public void run() {

try {

for (int i = 0; i 5; i++) {

System.out.println("進程" + i);

Thread.sleep(3000);

}

} catch (InterruptedException e) {

// TODO: handle exception

System.out.println("Thread has wrong" + e.getMessage());

}

System.out.println("退出第二個進程");

}

public static void main(String[] args) {

new threadDemo2();

}

}

JAVA程序設計,多線程,求大神給一份可運行的代碼

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

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

}???

}

網頁標題:java多線程實用代碼 java多線程實用代碼編譯
網頁地址:http://chinadenli.net/article40/hpioeo.html

成都網站建設公司_創(chuàng)新互聯(lián),為您提供App設計、做網站、關鍵詞優(yōu)化、移動網站建設、網站維護

廣告

聲明:本網站發(fā)布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內容未經允許不得轉載,或轉載時需注明來源: 創(chuàng)新互聯(lián)

成都定制網站建設