java 板


LINE

我想自行练习写一个多执行绪, 让args吃两个参数-一个决定动态生成几个Thread-一个是整数N 把整数N平分给每个Thread去计算总和最後再把所有Thread算好的部分加总起来 但我的程式遇到一点问题, 我希望呼叫check方法时可以取得所有thread算完的总和,请问该怎麽做呢????? public class Project { /** * @param args the command line arguments */ public static void main(String[] args) { // TODO code application logic here String temp1=""; String temp2=""; int ThreadNum=0; int n=0; temp1 = args[0]; temp2 = args[1]; ThreadNum = Integer.parseInt(temp1); n = Integer.parseInt(temp2); Show ba = new Show(); Counting[] p = new Counting[ThreadNum+1]; Thread[] t = new Thread[p.length]; int total =0; for(int i=1;i<p.length;i++) { int m = (i-1)*(n/ThreadNum)+1; int s = i*(n/ThreadNum); String Name = "t["+i+"]"; p[i] = new Counting(ba,Name,m,s); t[i] = new Thread(p[i]); t[i].start(); System.out.println(ba.total); } //System.out.println(); //System.out.println("temp="+temp); } } class Show { public static int total ; public static int last =total; public void save(int x) { total+=x; //System.out.println("now total:"+total); } public void check() { System.out.println(total); //return total; } } class Counting implements Runnable { private int start; private int end; public int sum; private String name; Show ba; public int temp=0; Counting (Show ba,String n,int m,int s) { this.ba = ba; name = n; start = m; end = s; } public synchronized void run() { System.out.println(name+"\tstarting"); for(int a = start;a<=end;a++) { sum+=a; } //System.out.println(t[0].current.getName()); //System.out.println(start+"\t"+end+"\t"+sum); //System.out.println("-----------------"); ba.save(sum); temp = sum; //System.out.println(ba.check()); ba.check(); System.out.println(name+"\tover"); } } --



※ 发信站: 批踢踢实业坊(ptt.cc)
◆ From: 140.121.196.35
1F:推 swpoker:你在show那边做一个计数的就可以了 05/07 16:05
2F:→ doggingg:有阿,可是我要怎麽才能在所有THREAD执行完後再取结果值 05/07 16:16
3F:推 LaPass:丢个block卡住他,另外thing in java有讲另外一种,专门为 05/07 16:29
4F:推 LaPass:计算=>取值用的执行绪物件,不过我手边没书.... 05/07 16:32
5F:推 Chikei:CountDownLatch 05/07 16:36
6F:推 mars90226:wait()执行绪? 05/07 18:36
7F:→ doggingg:谢谢大家~我作好了 05/07 18:57
8F:→ H45:做完了?我看这程式有Race Condition,也解决了吗? 05/07 22:51
9F:推 PsMonkey:违反版规 4.1,锁文软性惩罚 05/08 10:41
10F:→ PsMonkey:讲错了,是 4.2 XD 05/08 10:42
11F:→ doggingg:恩~在下方补上join就可以同步了,谢谢 05/08 14:20







like.gif 您可能会有兴趣的文章
icon.png[问题/行为] 猫晚上进房间会不会有憋尿问题
icon.pngRe: [闲聊] 选了错误的女孩成为魔法少女 XDDDDDDDDDD
icon.png[正妹] 瑞典 一张
icon.png[心得] EMS高领长版毛衣.墨小楼MC1002
icon.png[分享] 丹龙隔热纸GE55+33+22
icon.png[问题] 清洗洗衣机
icon.png[寻物] 窗台下的空间
icon.png[闲聊] 双极の女神1 木魔爵
icon.png[售车] 新竹 1997 march 1297cc 白色 四门
icon.png[讨论] 能从照片感受到摄影者心情吗
icon.png[狂贺] 贺贺贺贺 贺!岛村卯月!总选举NO.1
icon.png[难过] 羡慕白皮肤的女生
icon.png阅读文章
icon.png[黑特]
icon.png[问题] SBK S1安装於安全帽位置
icon.png[分享] 旧woo100绝版开箱!!
icon.pngRe: [无言] 关於小包卫生纸
icon.png[开箱] E5-2683V3 RX480Strix 快睿C1 简单测试
icon.png[心得] 苍の海贼龙 地狱 执行者16PT
icon.png[售车] 1999年Virage iO 1.8EXi
icon.png[心得] 挑战33 LV10 狮子座pt solo
icon.png[闲聊] 手把手教你不被桶之新手主购教学
icon.png[分享] Civic Type R 量产版官方照无预警流出
icon.png[售车] Golf 4 2.0 银色 自排
icon.png[出售] Graco提篮汽座(有底座)2000元诚可议
icon.png[问题] 请问补牙材质掉了还能再补吗?(台中半年内
icon.png[问题] 44th 单曲 生写竟然都给重复的啊啊!
icon.png[心得] 华南红卡/icash 核卡
icon.png[问题] 拔牙矫正这样正常吗
icon.png[赠送] 老莫高业 初业 102年版
icon.png[情报] 三大行动支付 本季掀战火
icon.png[宝宝] 博客来Amos水蜡笔5/1特价五折
icon.pngRe: [心得] 新鲜人一些面试分享
icon.png[心得] 苍の海贼龙 地狱 麒麟25PT
icon.pngRe: [闲聊] (君の名は。雷慎入) 君名二创漫画翻译
icon.pngRe: [闲聊] OGN中场影片:失踪人口局 (英文字幕)
icon.png[问题] 台湾大哥大4G讯号差
icon.png[出售] [全国]全新千寻侘草LED灯, 水草

请输入看板名称,例如:iOS站内搜寻

TOP