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

以下java代碼輸出線程,每一個java線程可以看成由代碼

誰幫我實現(xiàn)以下Java中的線程交替執(zhí)行控制,要求按順序輸出1到100,代碼如下。

稍微修改下?? 未測試

為利辛等地區(qū)用戶提供了全套網(wǎng)頁設(shè)計制作服務(wù),及利辛網(wǎng)站建設(shè)行業(yè)解決方案。主營業(yè)務(wù)為做網(wǎng)站、網(wǎng)站制作、利辛網(wǎng)站設(shè)計,以傳統(tǒng)方式定制建設(shè)網(wǎng)站,并提供域名空間備案等一條龍服務(wù),秉承以專業(yè)、用心的態(tài)度為用戶提供真誠的服務(wù)。我們深信只要達(dá)到每一位用戶的要求,就會得到認(rèn)可,從而選擇與我們長期合作。這樣,我們也可以走得更遠(yuǎn)!

package?com.leejiliang.HomeworkDemo2;

/**

*?定義打印輸出類,定義兩個數(shù)據(jù)輸出方法,分別用于輸出偶數(shù)和奇數(shù)

*?

*?@author?Administrator?even偶數(shù)?uneven奇數(shù)

*/

class?PrintNumber?{

boolean?isEven?=?true;

public?void?printEven()?{

for?(int?i?=?0;?i?=?100;?i?=?i?+?2)?{

if?(i?%?2?==?0)?{

synchronized?(this)?{

while?(!isEven)?{

try?{

this.wait();

}?catch?(InterruptedException?e)?{

//?TODO?Auto-generated?catch?block

e.printStackTrace();

}

}

System.out.println("偶數(shù):"?+?i);

isEven?=?false;

this.notify();

}

}

}

}

//?輸出1到100之間的奇數(shù)

public?void?printUnEven()?{

for?(int?i?=?1;?i?=?99;?i?=?i?+?1)?{

if?(i?%?2?!=?0)?{

synchronized?(this)?{

while?(isEven)?{

try?{

this.wait();

}?catch?(InterruptedException?e)?{

//?TODO?Auto-generated?catch?block

e.printStackTrace();

}

}

System.out.println("奇數(shù):"?+?i);

isEven?=?true;

this.notify();

}

}

}

}

}

/**

*?

*?@author?Administrator?定義輸出偶數(shù)的線程類

*/

class?ThreadForEven?implements?Runnable?{

private?PrintNumber?pn?=?null;

public?ThreadForEven(PrintNumber?pn)?{

this.pn?=?pn;

}

@Override

public?void?run()?{

pn.printEven();

}

}

/**

*?定義輸出奇數(shù)的線程類

*?

*?@author?Administrator

*

*/

class?ThreadForUneven?implements?Runnable?{

private?PrintNumber?pn?=?null;

public?ThreadForUneven(PrintNumber?pn)?{

this.pn?=?pn;

}

@Override

public?void?run()?{

pn.printUnEven();

}

}

//?主函數(shù),執(zhí)行兩個線程

class?ZeroToOneHundred?{

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

PrintNumber?pn?=?new?PrintNumber();

ThreadForEven?te?=?new?ThreadForEven(pn);

ThreadForUneven?tu?=?new?ThreadForUneven(pn);

Thread?threadeven?=?new?Thread(te);

Thread?threaduneven?=?new?Thread(tu);

threadeven.start();

threaduneven.start();

}

}

java線程問題:為什么以下代碼調(diào)用同樣的線程輸出的結(jié)果不一樣呢?一個是輸出1-10,一個輸出的全是1

首先說用M定義的線程,這里M相當(dāng)于繼承線程方法的類,在這個類里給它定義了線程方法。當(dāng)在public方法里創(chuàng)建M的實例時,類M中的方法也被實例化了,所以當(dāng)用t.start();啟動線程時,是啟動的M中的線程。再說for中的,和上邊完全相反,這個線程只是開啟了線程,并沒有定義其它方法,當(dāng)程序運行時能和上邊程序同時運行,所以都是1,這是for循環(huán)的結(jié)果

java多線程編程代碼如下,輸出結(jié)果如下:

首先,你同步的是具體的某個Test實例, 對于那個實例來說,實際上只有一個線程訪問了那個代碼塊,但是sum和other卻是多個線程同時去進(jìn)行訪問,實際上這是不安全的,如果你想實現(xiàn)每次都輸出10000的效果,那么正確的應(yīng)該是在Test.class上加鎖,而不是獲取Test實例的鎖,修改后的代碼如下:

public?class?Test?extends?Thread?{

public?static?int?sum?=?10000;

public?static?int?other?=?0;

public?void?getMoney()?{

synchronized?(Test.class)?{

System.out.println(Thread.currentThread().getName()?+?"?開始執(zhí)行");

sum?=?sum?-?100;

System.out.println("sum-100");

other?=?other?+?100;

System.out.println("other+100");

System.out.println(sum?+?other);

System.out.println(Thread.currentThread().getName()?+?"?執(zhí)行完成");

}

}

public?void?run()?{

getMoney();

}

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

Thread?t[]?=?new?Thread[10];

for?(int?i?=?0;?i?=?9;?i++)?{

t[i]?=?new?Test();

t[i].start();

}

}

}

// 上面代碼能得到你的結(jié)果

分享題目:以下java代碼輸出線程,每一個java線程可以看成由代碼
本文URL:http://chinadenli.net/article47/dseiihj.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)頁設(shè)計公司網(wǎng)站改版定制開發(fā)全網(wǎng)營銷推廣外貿(mào)網(wǎng)站建設(shè)

廣告

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

商城網(wǎng)站建設(shè)