Get Total Sum Using Multithread Programming
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的更多相关文章
- geeksforgeeks@ Minimum sum partition (Dynamic Programming)
http://www.practice.geeksforgeeks.org/problem-page.php?pid=166 Minimum sum partition Given an array, ...
- 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 ...
- 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 ...
- Dynamic Programming: From novice to advanced
作者:Dumitru 出处:http://community.topcoder.com/tc?module=Static&d1=tutorials&d2=dynProg An impo ...
- Java 中的函数式编程(Functional Programming):Lambda 初识
Java 8 发布带来的一个主要特性就是对函数式编程的支持. 而 Lambda 表达式就是一个新的并且很重要的一个概念. 它提供了一个简单并且很简洁的编码方式. 首先从几个简单的 Lambda 表达式 ...
- 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 ...
- [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 ...
- hdu 1258 Sum It Up(dfs+去重)
题目大意: 给你一个总和(total)和一列(list)整数,共n个整数,要求用这些整数相加,使相加的结果等于total,找出所有不相同的拼凑方法. 例如,total = 4,n = 6,list = ...
- leetcode problem sum
2. Add Two Numbers You are given two linked lists representing two non-negative numbers. The digits ...
随机推荐
- salesforce lightning零基础学习(四) 事件(component events)简单介绍
lightning component基于事件驱动模型来处理用户界面的交互.这种事件驱动模型和js的事件驱动模型也很相似,可以简单的理解成四部分: 1.事件源:产生事件的地方,可以是页面中的输入框,按 ...
- LeetCode Javascript实现 100. Same Tree 171. Excel Sheet Column Number
100. Same Tree /** * Definition for a binary tree node. * function TreeNode(val) { * this.val = val; ...
- Nginx多虚拟主机下泛域名配置
http://www.tuicool.com/articles/F3Azuq 近上一个应用,让用户可以自定义二级域名,所以要配置一个泛域名来解析用户的自定义域名. 首先来说说nginx下的泛域名配置 ...
- 巩固java(三)---java修饰符
正文: 下面的表格列出了java中修饰符的一些信息: 修饰符名称 类型 类 变量 方法 abstract 非访问控制符 抽象类 -- 抽象方法 final ...
- sublime text3汉化
注意在安装 sublime text3 时勾选Add to explorer context menu,这样在右键单击文件时就可以直接使用Sublime Text打开. 下载Package Contr ...
- javascript进阶
BOM操作 <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <met ...
- Java 读书笔记 (七) 变量
Java语言中, 所有的变量在使用前必须声明.声明变量的基本格式: type identifier [ =value],[,identifier [=value]...]; 声明变量的实例,有些包含了 ...
- Java开源生鲜电商平台-异常模块的设计与架构(源码可下载)
Java开源生鲜电商平台-异常模块的设计与架构(源码可下载) 说明:任何一个软件系统都会出现各式各样的异常与错误,我们需要根据异常的情况进行捕获与分析,改善自己的代码,让其更加的稳定的,快速的运行,那 ...
- 理解Go Context机制
1 什么是Context 最近在公司分析gRPC源码,proto文件生成的代码,接口函数第一个参数统一是ctx context.Context接口,公司不少同事都不了解这样设计的出发点是什么,其实我也 ...
- BZOJ_3872_[Poi2014]Ant colony_dfs
BZOJ_3872_[Poi2014]Ant colony_dfs Description There is an entrance to the ant hill in every chamber ...