In: Computer Science
I'm generating a random number every 10 second; In arraylist I'm Storing and Updating the number every 10 second when I generate new number. However, my goal to call the update number from arraylist in another class main method. I would apperciate any help.
//this is the class I'm generating a random number every 10 second and Storing and Updating the number in arraylist.
package com.singltondesignpattern;
import java.util.ArrayList;
import java.util.Random;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
public class EverySecond {
public static int genrand() {
Random number =new Random();
int rand=number.nextInt(1000);
return rand;
}
public static void getrand(int rand) {
ArrayList <Integer> randomstorage = new ArrayList<Integer>();
randomstorage.add(rand);
System.out.println("Array value "+randomstorage);
}
public static void main(String[] args) throws Exception {
Runnable helloRunnable = new Runnable() {
public void run() {
int CurrentNum=genrand(); //create a random number
System.out.println("generate number ==== "+CurrentNum);
getrand(CurrentNum); //update it in array list
}
};
ScheduledExecutorService executor = Executors.newScheduledThreadPool(1);
executor.scheduleAtFixedRate(helloRunnable, 0, 10, TimeUnit.SECONDS);
}
}
//this is the class I want to get the update number from the arraylist
public class getTheUpdate {
public static void main(String[] args) throws Exception {
EverySecond everySecond = new EverySecond();
everySecond.getrand(0);
}
}
//EverySecond.java class
import java.util.ArrayList;
import java.util.Random;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
public class EverySecond {
//declare the arraylist publically
static ArrayList<Integer> randomstorage = new
ArrayList<Integer>();
public static int genrand() {
Random number = new Random();
//generate random number
int rand = number.nextInt(1000);
//return generated random number
return rand;
}
public static void getrand(int rand) {
//initialise the arraylist
randomstorage = new ArrayList<Integer>();
//add new generated random number in arraylist
randomstorage.add(rand);
System.out.println("Array value " + randomstorage);
}
public static void main(String[] args) throws Exception {
Runnable helloRunnable = new Runnable() {
public void run() {
int CurrentNum = genrand(); //create a random number
System.out.println("generate number ==== " + CurrentNum);
getrand(CurrentNum); //update it in array list
}
};
ScheduledExecutorService executor =
Executors.newScheduledThreadPool(1);
//schedule thread for evry 10 seconds
executor.scheduleAtFixedRate(helloRunnable, 0, 10,
TimeUnit.SECONDS);
}
}
//getTheUpdate.java Where you can acess the EverySecond.java
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
public class getTheUpdate {
public static void main(String[] args) throws Exception {
//create object of EverySecond class
EverySecond everySecond = new EverySecond();
//or you can create another method to generate random number
every 10 second and pass the number to update in arraylist
Runnable helloRunnable = new Runnable() {
public void run() {
int CurrentNum = everySecond.genrand(); //create a random number
// System.out.println("generate number ==== " +
CurrentNum);
everySecond.getrand(CurrentNum); //update it in array list
System.out.println("This is updated number from ArrayList: " +
everySecond.randomstorage.get(0));
}
};
ScheduledExecutorService executor =
Executors.newScheduledThreadPool(1);
//schedule thread for evry 10 seconds
executor.scheduleAtFixedRate(helloRunnable, 0, 10,
TimeUnit.SECONDS);
}
}