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 ...
随机推荐
- Clloection接口 与List接口
collection接口: collection是单列集合接口的根接口,该接口中又包含了多个集合接口,collection接口提供了很多操作集合的方法,比如添加元素的方法,删除元素的方法,修改元素的方 ...
- 工作流引擎Activiti 专题
https://github.com/Activiti/Activiti Quick Start Guide This quick start assumes: Familiarity with Ma ...
- matplotlib使用时报错RuntimeError: Python is not installed as a framework(一)
笔者在第一次安装matplotlib后运行时出现报错. import matplotlib as mlb from matplotlib import pylab as pl x = [1,3,5,7 ...
- 富文本编辑器UEditor自定义工具栏(三、自定义工具栏功能按钮图标及工具栏样式简单修改)
导读 富文本编辑器UEditor提供丰富了定制配置项,如果想设置个性化的工具栏按钮图标有无办法呢?答案是肯定的!前两篇博文简要介绍了通过将原工具栏隐藏,在自定义的外部按钮上,调用UEditor各命令实 ...
- authorizations.go
{ return nil, fmt.Errorf("invalid TTL %d (must be >0)", authState.TTL) } ...
- BZOJ_2152_聪聪可可_点分治
BZOJ_2152_聪聪可可_点分治 Description 聪聪和可可是兄弟俩,他们俩经常为了一些琐事打起来,例如家中只剩下最后一根冰棍而两人都想吃.两个人都想玩儿电脑(可是他们家只有一台电脑)…… ...
- linux学习笔记基础篇(一)
一.IP操作 1.临时修改IP ,执行命令 ifconfig 网卡名称 新ip :例如 ifconfg nescc 192.168.1.110 ,重启失效 2.重启网络 ,执行命令 syste ...
- linux 挂载共享文件夹
1.背景 通常会有这样的场景,开发人员在Windows编写代码,然后放在linux环境编译,我们通过mount命令就可以实现将代码直接挂到linux环境上去,使Windows上的共享文件夹就像linu ...
- 服务网关基于RPC的用法
企业为了保护内部系统的安全性,内网与外网都是隔离的,企业的服务应用都是运行在内网环境中,为了安全的考量,一般都不允许外部直接访问.API网关部署在防火墙外面,起到一层挡板作用,内部系统只接受API网关 ...
- 死磕 java集合之SynchronousQueue源码分析
问题 (1)SynchronousQueue的实现方式? (2)SynchronousQueue真的是无缓冲的吗? (3)SynchronousQueue在高并发情景下会有什么问题? 简介 Synch ...