Open zhamri opened 4 years ago
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package realtime;
import java.util.Random;
/**
*
* @author master lab
*/
public class calculator extends Thread{
@Override
public void run(){
long start = System.currentTimeMillis();
Random r = new Random();
int randomlist[] = new int[10];
for(int i = 0;i<randomlist.length;i++){
int randomint = r.nextInt();
randomlist[i] = randomint;
System.out.println("These ten number are : "+randomint);
}
int sum = 0;
for(int i :randomlist){
sum+=i;
}
System.out.println("The sum of ten number are : "+sum);
long end = System.currentTimeMillis();
float sec = (end -start)/1000F;
System.out.println("Execution Time: "+sec+" seconds");
}
public static void main(String[] args){
Thread calculator = new calculator();
calculator.start();
}
}
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package Issue;
/**
*
* @author MICKY
*/
import java.util.Random;
class issueEleven{
public static void main(String[] args) throws InterruptedException{
Random r = new Random();
System.out.println("The ten random values are: ");
int sum=0;
for(int i=0; i<10; i++){
int randomint = r.nextInt(10);
System.out.print(randomint+",");
sum = sum + randomint ;
}
System.out.println("Sum = " + sum);
long start = System.currentTimeMillis();
Thread.sleep(2000);
long elapsedTimeMillis = System.currentTimeMillis() - start;
System.out.println("Execution time: "+elapsedTimeMillis);
}
}
TAN WEI DI 259296
import java.util.Random;
public class SequentialSum {
public static void main(String[] args){
long startTime = System.currentTimeMillis();
int total=0;
int[] arrNumber = new int[10];
for(int i=0; i <arrNumber.length;i++){
arrNumber[i] = new Random().nextInt(100);
System.out.println("Number "+(i+1)+" : "+arrNumber[i]);
total += arrNumber[i];
}
System.out.println("\nTotal = "+total);
long endTime = System.currentTimeMillis();
long timeElapsed = endTime - startTime;
System.out.println("\nExecution time in milliseconds: " + timeElapsed);
}
}
Tan Cheng Yi 253814
import java.util.Random;
public class sequentialSum {
public static void main(String[] args) {
Random rand = new Random();
int[] arr=new int[10];
for (int i = 0; i < arr.length; i++){
arr[i]=rand.nextInt();
}
long start = System.currentTimeMillis();
sum s=new sum();
System.out.println("Total: "+s.add(arr));
long end=System.currentTimeMillis();
System.out.println("Execution Time in Milisecond: " + (end-start));
}
}
class sum extends Thread{
public int add(int[] arr){
int total=0;
for (int i = 0; i < arr.length; i++){
total=total+arr[i];
}
return total;
}
}
Ong Xun Sheng 253881
package com.xunsheng;
import java.util.Random;
public class Main {
public static void main(String[] args) {
// write your code here
long start = System.currentTimeMillis();
int numbers[] = new int[10];
Random random = new Random();
for(int i = 0; i < 10; i++){
int num = random.nextInt();
numbers[i] = num;
}
SequentialSum sum = new SequentialSum();
System.out.println("Total : " + sum.sum(numbers));
long end = System.currentTimeMillis();
System.out.println("Time taken : " + (end-start));
}
}
class SequentialSum {
public int sum(int[] nums) {
int total = 0;
for (int i = 0; i < nums.length; ++i) {
total += nums[i];
}
return total;
}
}
import java.util.Random;
public class SequentialSum
{
public static void main(String[] args)
{
Random r = new Random();
int sum=0;
for(int i=0; i<10; i++)
{
int a = r.nextInt(10);
System.out.println("+"+a);
sum = sum + a ;
}
System.out.println("Sum = " + sum);
long start = System.currentTimeMillis();
System.out.println("Total Execution Time For Sequential Sum: " +(System.currentTimeMillis() - start) + " ms");
}
}
import java.util.Random;
public class week_08 {
public static void main(String[] args)throws InterruptedException{
Random r = new Random();
System.out.println("The ten random values are: ");
int sum=0;
for(int i=0; i<10; i++) {
int randomInts = r.nextInt(10);
System.out.print(randomInts+" , ");
sum = sum + randomInts;
}
{
System.out.println("Sum = " + sum);
long start = System.currentTimeMillis();
Thread.sleep(2000);
long elapsedTimeMillis = System.currentTimeMillis() - start;
System.out.println("Execution time: "+elapsedTimeMillis);
}
}
}
package sequentialsum;
import java.util.Random;
public class SequentialSum {
public static void main(String[] args) throws InterruptedException {
long start = System.currentTimeMillis();
Random r = new Random();
System.out.println("The ten random values are: ");
int sum = 0;
for (int i = 0; i <= 10; i++) {
int randomint = r.nextInt(10);
System.out.print(" " + randomint);
sum = sum + randomint;
Thread.sleep(60);
}
long end = System.currentTimeMillis();
System.out.println(" Sum = " + sum);
float millisec = (end - start);
System.out.println(millisec + " Milliseconds");
}
}
import java.util.Random;
public class RandomSeq { public static void main(String[]args) throws InterruptedException{ System.out.println("Genarate 10 random number in range 1..10"); long startTime = System.currentTimeMillis(); Random random = new Random(); int sum = 0; for (int i = 1; i <= 10; i++){ int rand = random.nextInt(10); System.out.println("Genarate : " +rand); sum = sum + rand; } System.out.println("Sum Of Random = " +sum);
long endTime = System.currentTimeMillis(); long timeElapsed = endTime - startTime; System.out.println("\nExecution time in milliseconds: " + timeElapsed);
System.out.println("Done");
}
}
import java.util.Random;
public class Example { public static void main(String[] args) throws Exception {
RandomNumbers();
long start = System.currentTimeMillis();
Thread.sleep(200);
long end = System.currentTimeMillis();
float milli = (end - start);
System.out.println(milli+ " miliseconds");
}
private static void RandomNumbers() {
Random r = new Random();
System.out.println("The ten random values are: ");
int[] values = new int[10];
for (int i = 0; i < 10; i++) {
int randomint = r.nextInt(10);
values[i] = randomint;
System.out.print(" " + randomint);
}
int sum = 0;
for (int i : values) {
sum += i;
}
System.out.println("\nSum = " + sum);
}
}
import java.util.Random;
public class Exercise11 {
public static void main(String[] args) throws InterruptedException {
long startTime = System.currentTimeMillis();
Random r = new Random();
int sum = 0;
for (int iCount = 1; iCount <= 10; iCount++) {
int randomNumber = r.nextInt();
System.out.println("Random No " + iCount + ": " + randomNumber);
sum = sum + randomNumber;
}
System.out.println("Total = " + sum);
long endTime = System.currentTimeMillis();
float millisec = (endTime - startTime);
System.out.println("Elapsed time in milliseconds: " + millisec);
}
}
package javaapplication5;
import java.util.Random;
public class SequentialSum {
public static void main(String[] args) {
long starttime = System.currentTimeMillis();
int sum = 0;
Random random = new Random();
int array[]=new int[10];
for (int i = 0; i < array.length; i++){
array[i]=random.nextInt(10);
System.out.println(array[i]);
sum += array[i];
}
System.out.println("Sum : "+sum);
long endtime=System.currentTimeMillis();
System.out.println("Execution Time in Milisecond: " + (endtime-starttime));
}
}
package issues11;
import static java.lang.Thread.sleep;
import java.util.Random;
import java.util.concurrent.TimeUnit;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
*
* @author Ahmed_Bawazir
*/
public class Issues11 {
/**
* @param args the command line arguments
*/
public static void main(String[] args) throws InterruptedException {
// TODO code application logic here
MyThread t1 = new MyThread();
t1.start();
}
}
class MyThread extends Thread {
public void run() {
Random r = new Random();
long startTime = System.nanoTime();
int j = 0, sum = 0;
for (int i = 0; i < 10; i++) {
j = r.nextInt(10);
try {
TimeUnit.SECONDS.sleep((long) 0.1);
} catch (InterruptedException ex) {
Logger.getLogger(MyThread.class.getName()).log(Level.SEVERE, null, ex);
}
sum = sum + j;
System.out.println(j);
}
System.out.println("sum = " + sum);
long endTime = System.nanoTime();
long timeElapsed = endTime - startTime;
System.out.println("Execution time in milliseconds: " + timeElapsed / 1000000);
}
}
package Issue11;
import java.util.Scanner;
import java.util.concurrent.Callable;
import java.util.Random;
public class Main {
private static Scanner sc = new Scanner(System.in);
private static Callable callable = new randomNumber();
public static void main(String[] args) throws Exception {
System.out.print("Enter how many random numbers need to be sum: ");
int num = sc.nextInt();
System.out.println();
System.out.println("Sequential Sum");
long start = System.currentTimeMillis();
for (int i = 1; i <= num; i++) {
int i1 = randomNumber.getN();
new plus(i1);
if (i == num) {
System.out.print(i1 + " = ");
} else {
System.out.print(i1 + " + ");
}
}
long finish = System.currentTimeMillis();
System.out.println(plus.aDouble);
System.out.println("Time taken: " + (finish-start) + "ms");
System.out.println();
plus.aDouble = 0;
System.out.println("Parallel Sum");
long l = System.currentTimeMillis();
for (int i = 1; i <= num; i++) {
int i1 = (int) callable.call();
new plus(i1);
if (i == num) {
System.out.print(i1 + " = ");
} else {
System.out.print(i1 + " + ");
}
}
long l1 = System.currentTimeMillis();
System.out.println(plus.aDouble);
System.out.println("Time taken: " + (l1-l) + "ms");
}
}
class randomNumber extends Random implements Callable<Integer> {
static int n = 0;
private static Random random = new Random();
static int getN() {
n = random.nextInt(100);
return n;
}
@Override
public Integer call() {
n = random.nextInt(100);
return n;
}
}
class plus {
static int aDouble = 0;
plus(int i) {
aDouble += i;
}
}
import java.util.Random;
public class exercise {
public exercise(){}
public static void main(String[] args){
long startTime = System.currentTimeMillis();
exercise ext = new exercise();
ext.randnumber();
long endTime = System.currentTimeMillis();
System.out.println("That took " + (endTime - startTime) + " milliseconds");
}
public void randnumber(){
Random r = new Random();
System.out.println("The ten random values: ");
int[] values = new int[10];
for(int i = 0; i < 10; i++) {
int randomint = r.nextInt(10);
values[i] = randomint;
System.out.println(randomint);
}
int sum = 0;
for(int i : values) {
sum += i;
}
System.out.println("Sum of ten random numbers = " + sum);
}
}
import java.util.Random;
public class ran extends Thread{
public void run(){
Random r = new Random();
int randomlist[] = new int[10];
for (int i = 0;i < randomlist.length;i++){
int randomint = r.nextInt(100);
randomlist[i] = randomint;
System.out.println("Number: "+randomint);
}
int sum = 0;
for (int i : randomlist){
sum+=i;
}
System.out.println("Sum: "+sum);
}
public static void main(String[] args) throws InterruptedException {Thread ran = new ran();
ran.start();
Thread.sleep(500);
}
}
import java.util.Random;
public class RandomNumbers{
public static void main(String[] args){
long startTime = System.currentTimeMillis();
int total=0;
int[] arrNumber = new int[10];
for(int i=0; i <arrNumber.length;i++){
arrNumber[i] = new Random().nextInt(10);
System.out.println("Number "+(i+1)+" : "+arrNumber[i]);
total += arrNumber[i];
}
System.out.println("\nTotal = "+total);
long endTime = System.currentTimeMillis();
long timeDiff = endTime - startTime;
System.out.println("Elapsed time in milliseconds: " + timeDiff);
}
}
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.concurrent.TimeUnit;
public class seqSum extends Thread {
public static void main(String[]args) {
Random rand = new Random( );
List numList = new ArrayList();
int i, total = 0;
System.out.print("10 random numbers are ");
for (int x = 0; x < 10; x++) {
i = rand.nextInt(10);
total = total + i;
numList.add(total);
System.out.print(i+" ");
}
long startTime = System.currentTimeMillis();
numList.stream().forEach(y -> methodToTime((Integer) y));
long endTime = System.currentTimeMillis();
double sequentialStreamTimetaken = (endTime - startTime) / 1000;
System.out.println("\nTotal : "+total);
System.out.println("Time required with stream() : " + sequentialStreamTimetaken + "ms");
}
private static void methodToTime(int num) {
try {
TimeUnit.SECONDS.sleep(3);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
import java.util.Random;
public class ranthread implements Runnable {
int a;
private Object args;
public ranthread(int a) {
this.a = a;
}
public void run() {
addRondom();
}
public void addRondom() {
Random rand = new Random();
int n = rand.nextInt(10) + 1;
System.out.println("random number: " + n);
synchronized (this) {
a += n;
}
}
}
public class add {
public static void main(String[] args) {
int base = 0;
ranthread sum2 = new ranthread(base);
Thread t1 = new Thread(sum2);
Thread t2 = new Thread(sum2);
Thread t3 = new Thread(sum2);
Thread t4 = new Thread(sum2);
Thread t5 = new Thread(sum2);
Thread t6 = new Thread(sum2);
Thread t7 = new Thread(sum2);
Thread t8 = new Thread(sum2);
Thread t9 = new Thread(sum2);
Thread t10 = new Thread(sum2);
t1.start();
t2.start();
t3.start();
t4.start();
t5.start();
t6.start();
t7.start();
t8.start();
t9.start();
t10.start();
try {
t1.join();
t2.join();
t3.join();
t4.join();
t5.join();
t6.join();
t7.join();
t8.join();
t9.join();
t10.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.print("final result: " + sum2.a);
}
}
![Capture](https://user-images.githubusercontent.com/55201372/69003821-747c8d80-0943-11ea-8118-a1bcb74b6e81.PNG)
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package Thread07;
import java.util.Random;
/**
*
* @author WANG LUO
*/
public class calculator extends Thread {
int sum;
public calculator(int a){
this.sum = a;
}
public void getSum(){
Random r = new Random();
int num = r.nextInt(100);
System.out.println(Thread.currentThread().getName()+": "+num);
synchronized(this){
sum += num;
}
}
@Override
public void run(){
try{
getSum();
}catch(Exception e){
e.printStackTrace();
}
}
}
public class mainTest extends Thread{
/**
*
* @param args
*/
public static void main(String[] args) throws InterruptedException{
calculator c = new calculator(0);
Thread t1 = new Thread(c);
Thread t2 = new Thread(c);
Thread t3 = new Thread(c);
Thread t4 = new Thread(c);
Thread t5 = new Thread(c);
Thread t6 = new Thread(c);
Thread t7 = new Thread(c);
Thread t8 = new Thread(c);
Thread t9 = new Thread(c);
Thread t10 = new Thread(c);
try{
t1.start();
t1.join();
t2.start();
t2.join();
t3.start();
t3.join();
t4.start();
t4.join();
t5.start();
t5.join();
t6.start();
t6.join();
t7.start();
t7.join();
t8.start();
t8.join();
t9.start();
t9.join();
t10.start();
t10.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("sum"+c.a);
long start = System.currentTimeMillis();
Thread.sleep(2000);
long end = System.currentTimeMillis();
float sec = (end -start)/1000F;
System.out.println("Execution Time: "+sec+" seconds");
}
}
Class Sequential
package com.baktajivan.sequential;
public class Sequential {
public static int sum(int [] arr){
int total =0;
for(int i=0;i<arr.length;i++)
total+=arr[i];
return total;
}
}
Class Main
package com.baktajivan.sequential;
import java.util.*;
public class Main {
public static void main(String [] args){
Random random = new Random();
int[] arr = new int[10];
for(int i=0; i<arr.length; i++){
arr[i] = random.nextInt(10)+1;
}//for
int total = Sequential.sum(arr);
long start = System.currentTimeMillis();
System.out.println("Total Sum: "+total);
System.out.println("Time taken: "+(System.currentTimeMillis()-start)+"ms");
}
}
Output:
Reference: https://eddmann.com/posts/parallel-summation-in-java/ https://github.com/zhamri/STIW3054-RT-Programming/tree/master/src/Week_13
import java.util.Random;
public class randomClass {
public static void main(String args[]) {
long startTime=System.currentTimeMillis();
int sumTotal=0;
int[] intRandom = new int[10];
for(int i=0; i <intRandom.length;i++){
intRandom[i] = new Random().nextInt(100);
System.out.println("Number "+(i+1)+" : "+intRandom[i]);
sumTotal += intRandom[i];
}
System.out.println("\nTotal = "+sumTotal);
long endTime = System.currentTimeMillis();
long timeElapsed = endTime - startTime;
System.out.println("\nExecution time in milliseconds: " + timeElapsed);
}
}
import java.util.Random;
public class SequentialSum {
public static void main(String[] args){
long startTime = System.currentTimeMillis();
int total=0;
int[] arrNumber = new int[10];
for(int i=0; i <arrNumber.length;i++){
arrNumber[i] = new Random().nextInt(100);
System.out.println("Number "+(i+1)+" : "+arrNumber[i]);
total += arrNumber[i];
}
System.out.println("\nTotal = "+total);
long endTime = System.currentTimeMillis();
long timeElapsed = endTime - startTime;
System.out.println("\nExecution time in milliseconds: " + timeElapsed);
}
}
import java.util.Random;
public class one {
public static void main(String[] args){
long startTime = System.currentTimeMillis();
int total=0;
int[] arrNumber = new int[10];
for(int i=0; i <arrNumber.length;i++){
arrNumber[i] = new Random().nextInt(100);
System.out.println("Number "+(i+1)+" : "+arrNumber[i]);
total += arrNumber[i];
}
System.out.println("\nTotal = "+total);
long endTime = System.currentTimeMillis();
long timeElapsed = endTime - startTime;
System.out.println("\nExecution time in milliseconds: " + timeElapsed);
}
}
Instruction
Write a Java program to sum 10 random numbers. Then calculate the execution time in milliseconds.
Submission