Write a program that finds the sum value in an array of ints using 4 threads. You may assume in your threaded code that the array has at least 4 elements.

~~~
public class Main {
public static void main(String[] args) throws InterruptedException {
int numOfThread = 4, dataLen = 4;
Calculator[] calculators = new Calculator[numOfThread]; Random random = new Random();
int[] data = new int[dataLen];
System.out.print("Random generate data:");
for (int i = 0; i < dataLen; i++) {
data[i] = random.nextInt(10);
System.out.print(data[i] + " ");
}
for (int i = 0; i < numOfThread; i++) {
calculators[i] = new Calculator(data, i * dataLen / numOfThread, (i + 1) * dataLen / numOfThread);
calculators[i].start();
}
int sum = 0;
for (int i = 0; i < numOfThread; i++) {
calculators[i].join();
sum += calculators[i].getAns();
}
System.out.println();
System.out.println("Total Sum:" + sum); }
}
class Calculator extends Thread {
private int[] data;
private int start, end, ans = 0; public Calculator(int[] data, int start, int end) {
this.data = data;
this.start = start;
this.end = end;
} @Override
public void run() {
for (int i = start; i < end; i++) {
ans += data[i];
}
} public int getAns() {
return ans;
}
}

Get Total Sum Using Multithread Programming的更多相关文章

  1. geeksforgeeks@ Minimum sum partition (Dynamic Programming)

    http://www.practice.geeksforgeeks.org/problem-page.php?pid=166 Minimum sum partition Given an array, ...

  2. Given a binary tree containing digits from0-9only, each root-to-leaf path could represent a number. An example is the root-to-leaf path1->2->3which represents the number123. Find the total sum of a

    class TreeNode { int val; TreeNode left; TreeNode right; TreeNode(int x) { val = x; } } public class ...

  3. Distribute numbers to two “containers” and minimize their difference of sum

    it can be solved by Dynamical Programming.Here are some useful link: Tutorial and Code: http://www.c ...

  4. Dynamic Programming: From novice to advanced

    作者:Dumitru 出处:http://community.topcoder.com/tc?module=Static&d1=tutorials&d2=dynProg An impo ...

  5. Java 中的函数式编程(Functional Programming):Lambda 初识

    Java 8 发布带来的一个主要特性就是对函数式编程的支持. 而 Lambda 表达式就是一个新的并且很重要的一个概念. 它提供了一个简单并且很简洁的编码方式. 首先从几个简单的 Lambda 表达式 ...

  6. Fork and Join: Java Can Excel at Painless Parallel Programming Too!---转

    原文地址:http://www.oracle.com/technetwork/articles/java/fork-join-422606.html Multicore processors are ...

  7. [LeetCode] Sum Root to Leaf Numbers 求根到叶节点数字之和

    Given a binary tree containing digits from 0-9 only, each root-to-leaf path could represent a number ...

  8. hdu 1258 Sum It Up(dfs+去重)

    题目大意: 给你一个总和(total)和一列(list)整数,共n个整数,要求用这些整数相加,使相加的结果等于total,找出所有不相同的拼凑方法. 例如,total = 4,n = 6,list = ...

  9. leetcode problem sum

    2. Add Two Numbers You are given two linked lists representing two non-negative numbers. The digits ...

随机推荐

  1. js判断浏览器是否支持flash的方法

    传统浏览器可以使用window.ActiveXObject检查浏览器是否启用相关的控件.检查浏览器是否启用flash控件,需要先检查浏览器是否支持ActiveXObject,可以使用typeof检查w ...

  2. 架构选型之Nodejs与Java

    前言: 身边越来越多的同事谈论Nodejs,谈其异步IO.事件回调.前后台统一一门语言,创业的朋友的第一个创业项目也选择了Nodejs,期望能够使用一种语言节省成本快速完成需求开发.与其他项目组的同事 ...

  3. python资料分享

    python入门资料分享:链接:https://pan.baidu.com/s/1aATizMh5e0ON6xfmtxXPzA  密码:m8bf 提高资料:链接:https://pan.baidu.c ...

  4. HTML5 CSS3专题 诱人的实例 CSS3打造百度贴吧的3D翻牌效果

    首先感谢w3cfuns的老师~ 今天给大家带来一个CSS3制作的翻牌效果,就是鼠标移到元素上,感觉可以看到元素背后的信息.大家如果制作考验记忆力的连连看.扑克类的游戏神马的,甚至给女朋友写一些话语,放 ...

  5. Golang 嵌套map赋值办法

    http://my.oschina.net/sol/blog/159060 m := map[string]map[string]string{} mm, ok := m["kkk" ...

  6. 译MassTransit 快速入门

    给我看代码! 下面是MassTransit的功能设置. public class YourMessage { public string Text { get; set; } } public cla ...

  7. mysql(mariadb)如何更改root密码

    mysql(或者mariadb,她是mysql的一个分支,完全开源,新版本的linux系统默认安装的是mariadb)如何更改root密码呢?我们主要介绍命令mysqladmin来实现. mysql( ...

  8. 【BZOJ 3697】采药人的路径

    题目链接: TP 题解: 调了好久233. 大概想一想就是树分,然后考虑这样路径(u,v)的特征,以根节点(root)切开,u到root的阴阳差值,和v到root巧合互为相反数,然后考虑要有一个点可作 ...

  9. POJ_3304_Segments_线段判断是否相交

    POJ_3304_Segments_线段判断是否相交 Description Given n segments in the two dimensional space, write a progra ...

  10. BZOJ_1433_[ZJOI2009]假期的宿舍_二分图匹配

    BZOJ_1433_[ZJOI2009]假期的宿舍_二分图匹配 题意: 学校放假了······有些同学回家了,而有些同学则有以前的好朋友来探访,那么住宿就是一个问题.比如A 和B都是学校的学生,A要回 ...