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 ...
随机推荐
- SQL*Loader FAQ
SQL*Loader FAQ: Contents [hide] 1 What is SQL*Loader and what is it used for? 2 How does one use th ...
- 近期写js库中遇到的一个判别的问题
最近在写一个自己的js库,正写到数组包,在里面定义了一个排序,只对纯数字数据进行排序的方法,但是在测试的时候发现一个很诡异的问题,那就是传入一个对象的时候,它没有返回erroemsg而是返回了对象,上 ...
- fixed元素随滚动条无抖动滚动
页面上用fixed定位一个元素,随滚动条滚动位置不变,最开始我只用了css给元素身上写上fixed属性,发现滚动时元素会发生抖动,随后我就在网上找到解决办法,封装了个方法,如下: Css部分 此部分是 ...
- Nginx安装及配置详解
nginx概述 nginx是一款自由的.开源的.高性能的HTTP服务器和反向代理服务器:同时也是一个IMAP.POP3.SMTP代理服务器:nginx可以作为一个HTTP服务器进行网站的发布处理,另外 ...
- pymongo连接MongoDB
导语 pymongo 是目前用的相对普遍一个python用来连接MongoDB的库,是工作中各种基本需求都能满足具体api可以参考 pymongo APIpymongo github 安装 Mongo ...
- JAXBContextAPI详解
javax.xml.bind类 JAXBContext java.lang.Object javax.xml.bind.JAXBContext public abstract class JAXBCo ...
- 用java代码将从数据库中取出的具有父子关系的数据转成json格式
思路:①.取出数据中的所有父节点放入一个集合中②.取出数据中所有为该父节点的子节点放入另一个集合中③.用到迭代的方法将子节点一层一层的遍历工具类:package com.assasion.test;i ...
- Oracle解锁scott账号
在安装Oracle的最后一步,有一个口令管理的操作,当时忘了给scott账号解锁了(Oracle为程序测试提供的一个普通账户,口令管理中可以对数据库用户设置密码,默认是锁定的).现在想给scott这个 ...
- mysql主从复制搭建
1.准备工作: 准备一台主服务器,我的IP地址为192.168.13.138,和一台从服务器:192.168.13.137,数据库版本一致,主从库都建好相应的库和表: 2.修改主从服务器的mysql配 ...
- BZOJ_1774_[Usaco2009 Dec]Toll 过路费_floyd
BZOJ_1774_[Usaco2009 Dec]Toll 过路费_floyd 题意: 跟所有人一样,农夫约翰以着宁教我负天下牛,休叫天下牛负我的伟大精神,日日夜夜苦思生 财之道.为了发财,他设置了一 ...