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. MongoDB的基本操作(一)

    一.简介       mongodb是是由C++语言编写的一个基于分布式文件存储的开源nosql数据库系统,支持文档和键值存储模型,拥有灵活的数据模型.可靠的性能,以及自动的吞吐容 量扩展功能.    ...

  2. 1. 开篇-springboot环境搭建

    最初学习strurs2时,虽然觉得也挺好用的,但也有一些不便的地方:1. 模型绑定必须要在Action中声明对应模型的成员变量:2. Action中对外提供调用的接口必须明确注明:3. 要声明一大堆的 ...

  3. spring的依赖注入是什么意思

    最近学习spring框架,对依赖注入有些模糊,遂上网翻阅资料,做了下列总结,原博客为CSDN 南夏的 spring的依赖注入是什么意思,侵删! Spring 能有效地组织J2EE应用各层的对象.不管是 ...

  4. ubuntu下dpkg、phantomjs安装包下载地址

    dpkg下载地址 http://download.chinaunix.net/download/0003000/2377.shtml phantomjs下载地址 https://bitbucket.o ...

  5. Python 模块详解及import本质

    同在当前目录下的模块和包导入 模块定义 本质就是.py结尾的python文件. 用来从逻辑上组织python代码(变量,函数,类,逻辑) 文件名: test.py;  对应的模块名 : test 模块 ...

  6. BZOJ_1180_[CROATIAN2009]OTOCI_LCT

    BZOJ_1180_[CROATIAN2009]OTOCI_LCT Description 给出n个结点以及每个点初始时对应的权值wi.起始时点与点之间没有连边.有3类操作:  1.bridge A ...

  7. B20J_1297_[SCOI2009]迷路_矩阵乘法

    B20J_1297_[SCOI2009]迷路_矩阵乘法 题意:有向图 N 个节点,从节点 0 出发,必须恰好在 T 时刻到达节点 N-1.总共有多少种不同的路径? 2 <= N <= 10 ...

  8. 求二维数组的最大子数组———曹玉松&&蔡迎盈

    继上节课老师让求了一维数组最大的子数组后,这节课堂上,老师加深了难度,给了一个二维数组,求最大子数组,开始觉得很容易,但是自己思考起来感觉这个算法很困难,既需要考虑数组直接的连续,又要求出最大的,老师 ...

  9. (2)STM32使用HAL库操作外部中断——理论讲解

    1.中断触发过程 对主程序压栈--把中断服务函数的地址写入到程序计数器(PC)--执行中断服务函数 2.中断向量表 中断服务函数的地址在STM32的手册上的中断向量表中(如下是一部分): 如上表所示, ...

  10. 【h5+c3】web前端实战项目、快装webapp手机案例源码

    快装WebApp项目(Web移动端开发案例)webapp移动端项目源码.html5+css3实战案例分享.微信端H5实例开发 简介快装WebApp是一个面向移动端的快速装修app,此项目为手机端:使用 ...