I'm generating a random number every 10 second and Storing and Updating the number every 10 second when I generate new number in UpdateInt method. However, In my main method when I called UpdateInt method it generate every 10 second new number but it doesn't update and store in the arraylist.
***I want the Runnable to be in the arraylist method. Not in main method. my goal is to send the arraylist that has the update number***
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();
//generate random number
int rand = number.nextInt(1000);
//return generated random number
return rand;
}
public static ArrayList<Integer> UpdateInt(){
ArrayList<Integer> Update = new ArrayList<Integer>();
Runnable helloRunnable = new Runnable() {
public void run() {
int CurrentNum = genrand(); //create a random number
System.out.println("generate number ==== " + CurrentNum);
Update.add(CurrentNum);//update it in array list
}
};
ScheduledExecutorService executor = Executors.newScheduledThreadPool(1);
//schedule thread for evry 10 seconds
executor.scheduleAtFixedRate(helloRunnable, 0, 10, TimeUnit.SECONDS);
return Update;
}
public static void main(String[] args) throws Exception {
ArrayList<Integer> getUpdate=UpdateInt();
for(int x=0;x<getUpdate.size();x++) {
System.out.println("Current Num = " + getUpdate.get(x));
}
}
}
Current Output:
generate number ==== 367
generate number ==== 944
It should be :
generate number ==== 367
Current Num = 367
generate number ==== 47
Current Num = 47
NOTE: ***I want the Runnable to be in the arraylist method. Not in main method. my goal is to send the arraylist that has the update number***
I should get the same number in main method when Ioop that I generated in UpdateInt method. Note: I want the Runnable() to be in arraylist method and return the arraylist that has the update number. I would apperciate any help.
Hi,
Please find updated code below.
For this since we return a value we need a callable wrapped around in runnable.
runnable is required to run at every 10 seconds.
Callable is required to return updated array.
As demonstrated in program below.
Main.java :
import java.util.ArrayList;
import java.util.Random;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.Callable;
import java.util.concurrent.FutureTask;
public class Main
{
public static void main(String[] args) throws Exception
{
ScheduledExecutorService executor =
Executors.newScheduledThreadPool(1);
//schedule thread for evry 10 seconds
//executor.schedule(new sampleCallableExample(), 1,
TimeUnit.MILLISECONDS);
executor.scheduleAtFixedRate(new sampleCallableExample(), 0, 10,
TimeUnit.SECONDS);
System.out.println("array size:"+globals.Update.size());
for(int x=0;x<globals.Update.size();x++)
{
System.out.println("Current Num = " + globals.Update.get(x));
}
}
}
Kindly declare Update array as global variable: inside new class globals as shown below:
import java.util.ArrayList;
public class globals
{
public static ArrayList<Integer> Update = new
ArrayList<Integer>();
}
sampleCallableExample.java :
import java.util.concurrent.Callable;
import java.util.concurrent.FutureTask;
import java.util.Random;
class sampleCallableExample implements Runnable
{
public static int genrand()
{
Random number = new Random();
//generate random number
int rand = number.nextInt(1000);
//return generated random number
return rand;
}
public Object call() throws Exception
{
//ArrayList<Integer> Update = new
ArrayList<Integer>();
int CurrentNum = genrand();
System.out.println("generate number ==== " + CurrentNum);
globals.Update.clear();
globals.Update.add(CurrentNum);
System.out.println("Number added");
return globals.Update;
}
// As seen run wraps call object
public void run()
{
try
{
Object o = call();
//System.out.println("Returned " + o);
}
catch (Exception e)
{
e.printStackTrace();
}
}
public sampleCallableExample()
{
try
{
run();
}
catch(Exception e)
{
}
}
}
output attached: You can see current num and generated number updated.
Thanks,
kindly upvote.
Get Answers For Free
Most questions answered within 1 hours.