java線程推塔代碼 java線程死鎖代碼

java線程的經典代碼

package threadgroup;

汝城網站建設公司創新互聯公司,汝城網站設計制作,有大型網站制作公司豐富經驗。已為汝城上1000家提供企業網站建設服務。企業網站搭建\外貿網站制作要多少錢,請找那個售后服務好的汝城做網站的公司定做!

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("在創建一個進程");

t2.start();

try {

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

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多線程例子,最好有代碼,謝謝啦!

package a.b.test;

import java.util.Date;

import java.util.concurrent.Callable;

import java.util.concurrent.ExecutorService;

import java.util.concurrent.Executors;

import java.util.concurrent.Future;

public class Calculate1000 implements CallableInteger{

public Calculate1000(){}

public Calculate1000(int a, int b){

this.a = a;

this.b = b;

}

int a;

int b;

/**

* @param args

* @throws Exception

*/

public static void main(String[] args) throws Exception {

//同步

Calculate1000 ca1 = new Calculate1000();

Date ds1 = new Date();

int result = 0;

for(int i = 1 ; i = 1000 ; i++){

result = ca1.add(i, result);

}

System.out.println(result);

System.out.println("同步用時" + (new Date().getTime() - ds1.getTime()) + "MS");

//異步

Date ds2 = new Date();

result = 0;

ExecutorService es = Executors.newFixedThreadPool(2);

FutureInteger future1 = es.submit(new Calculate1000(1,500));

FutureInteger future2 = es.submit(new Calculate1000(501,1000));

result = future1.get() + future2.get();

System.out.println(result);

System.out.println("異步用時" + (new Date().getTime() - ds2.getTime()) + "MS");

es.shutdown();

}

private int add(int a, int b) throws Exception{

Thread.sleep(10);

return a + b;

}

@Override

public Integer call() throws Exception {

int res = 0;

for(int i = a ; i = b ; i++){

res = this.add(res, i);

}

return res;

}

}

樓主你試一下這段代碼行不行,行的話請采納!

java多線程編程代碼如下,輸出結果如下:

首先,你同步的是具體的某個Test實例, 對于那個實例來說,實際上只有一個線程訪問了那個代碼塊,但是sum和other卻是多個線程同時去進行訪問,實際上這是不安全的,如果你想實現每次都輸出10000的效果,那么正確的應該是在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()?+?"?開始執行");

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()?+?"?執行完成");

}

}

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

}

}

}

// 上面代碼能得到你的結果

網站名稱:java線程推塔代碼 java線程死鎖代碼
文章分享:http://m.kartarina.com/article40/hjgoho.html

成都網站建設公司_創新互聯,為您提供網站營銷標簽優化用戶體驗虛擬主機App設計搜索引擎優化

廣告

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

成都seo排名網站優化
主站蜘蛛池模板: 国产色综合久久无码有码 | 毛片无码一区二区三区a片视频| 国产亚洲?V无码?V男人的天堂 | 亚洲日韩欧洲无码av夜夜摸| 日韩精品少妇无码受不了| 亚洲色无码国产精品网站可下载| 一区二区三区无码高清| 精品国产aⅴ无码一区二区| 日韩A无码AV一区二区三区| 精品无码一区二区三区电影| 综合国产在线观看无码| 无码里番纯肉h在线网站| 精品无码AV一区二区三区不卡| 亚洲AV无码一区二区三区在线观看| 无码人妻AV免费一区二区三区| 无码的免费不卡毛片视频| 无码无需播放器在线观看| 小13箩利洗澡无码视频网站| 亚洲AⅤ永久无码精品AA| 精品无码一区二区三区电影| 无码人妻精品中文字幕| 特级做A爰片毛片免费看无码 | 无码av专区丝袜专区| 无码GOGO大胆啪啪艺术| 亚洲中文字幕久久精品无码喷水| 无码人妻一区二区三区免费| 无码人妻丰满熟妇区免费| 中文午夜乱理片无码| 国产AV无码专区亚洲AV漫画 | 亚洲成AV人片天堂网无码| 无码日本精品XXXXXXXXX| 亚洲av成人无码网站…| 无码日本精品XXXXXXXXX| 午夜无码人妻av大片色欲| 成人年无码AV片在线观看| 无码h黄肉3d动漫在线观看| 国产日产欧洲无码视频| 午夜无码一区二区三区在线观看 | 亚洲av无码成人黄网站在线观看| 亚洲人成无码www久久久| 免费a级毛片无码av|