my goal is to generate a random number every 10 seconds and store in arraylist. when I generate the new number every 10 second I want to update the previous number from the arraylist. currently it gets the first current number in main method but when I store after 10 second i'm not getting the update current number. I would appreciate any help. NOTE: ***I want the Runnable to be in the same class, Not in main method. my goal is to send the arraylist that has the update number*** ***PLEASE I want to get the current number from main method loop*******
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
System.out.println("Start Loop : ");
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));
}
System.out.println("Close Loop : ");
}
}
package com.Test;
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 Update = new ArrayList();
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) {
}
}
}
package com.Test;
import java.util.ArrayList;
public class globals {
public static ArrayList Update = new ArrayList();
}
The Current output is:
Start Loop :
generate number ==== 992
Number added
array size:1
Current Num = 992
Close Loop :
generate number ==== 407
Number added
generate number ==== 32
Number added
However, It should be:
Start Loop :
generate number ==== 992
Number added
array size:1
Current Num = 992
Close Loop :
Start Loop :
generate number ==== 407
Number added
array size:1
Current Num = 407
Close Loop :
Start Loop :
generate number ==== 32
Number added
array size:1
Current Num = 32
Close Loop :
NOTE: ***I want the Runnable to be in the same class, Not in main method. my goal is to send the arraylist that has the update number*** ***PLEASE I want to get the current number from main method loop******
Hi,
I understand your concern. but please understand that if you keep the code in main method, there is no chance that the code in main method will be executed. becuase the code we are writing will be executed for every 10 seconds is teh Runnable class, not main method.
So to get the desired output, the possibility is to put the code related to main method in run method of runnable class so that for every 10 seconds run method being called by schedular so that we will get the desired output.
Please find the below code.
====================================================
sampleCallableExample.java
====================================================
package runnable;
import java.util.Random;
public class sampleCallableExample implements Runnable {
public static void main(String[] args) {
}
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 Update = new ArrayList();
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 {
System.out.println("Start Loop : ");
Object o =
call();
//System.out.println("Returned " + o);
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));
}
System.out.println("Close Loop : ");
} catch (Exception e) {
e.printStackTrace();
}
}
public sampleCallableExample() {
try {
run();
} catch (Exception e) {
}
}
}
=============================================
Main.java
=============================================
import java.util.ArrayList;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
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);
}
}
class globals {
public static ArrayList Update = new ArrayList();
}
Sample output:
I hope this helps you!!
Thanks.
my goal is to generate a random number every 10 seconds and store in arraylist. when...
my goal is to generate a random number every 10 seconds and store in arraylist. when I generate the new number every 10 second I want to update the previous number from the arraylist. currently it gets the first current number in main method but when I store after 10 second i'm not getting the update current number. I would appreciate any help. NOTE: ***I want the Runnable to be in the same class, Not in main method. my goal...
my goal is to generate a random number every 10 seconds and store in arraylist. when I generate the new number every 10 second I want to update the previous number from the arraylist. currently it gets the first number current num in main method but when I store after 10 second i'm not geting the update current number. NOTE: ***I want the Runnable to be in the arraylist method. Not in main method. my goal is to send the...
. In the method main, prompt the user for a non-negative integer and store it in an int variable num (Data validation is not required for the lab). Create an int array whose size equals num+10. Initialize the array with random integers between 0 and 50 (including 0 and excluding 50). Hint: See Topic 4 Decision Structures and File IO slides page 42 for how to generate a random integer between 0 (inclusive) and bound (exclusive) by using bound in...
I need help on creating a while loop for my Java assignment. I am tasked to create a guessing game with numbers 1-10. I am stuck on creating a code where in I have to ask the user if they want to play again. This is the code I have: package journal3c; import java.util.Scanner; import java.util.Random; public class Journal3C { public static void main(String[] args) { Scanner in = new Scanner(System.in); Random rnd = new Random(); System.out.println("Guess a number between...
9. The purpose of the following program is to generate 10 random integers, store them in an array, and then store in the freq frequency array, the number of occurrences of each number from 0 to 9. Can you find any errors in the program? If yes, correct them. #include <stdio.h> #include <stdlib.h> #include <time.h> #define SIZE 10 int main(void) int i, num, arr[SIZE], freq[SIZE]; srand (time (NULL)); arr[1] rand(); SIZE; for(i i 〈 i++) num arr[i]; freq[num]++; printf("InNumber occurrences...
How to measure the average performance of sorting algorithms with different sizes and numbers(with arrayList<Integer>) and compare with a graph? I have these methods: //This one receives one size(n) parameter public static ArrayList<Integer> RandomArray(int n){ ArrayList<Integer> randomArray = new ArrayList<Integer>(n); Random rand = new Random(); //--- random number rand.setSeed(System.currentTimeMillis()); for(int i = 0; i < n; i++ ) { //loop for creating...
Write a method public static ArrayList merge(ArrayList a, ArrayList b) that merges two array lists, alternating elements from both array lists. If one array list is shorter than the other, then alternate as long as you can and then append the remaining elements from the longer array list. For example, if a is 1 4 9 16 and b is 9 7 4 9 11 then merge returns the array list 1 9 4 7 9 4 16 9 11...
Create a program using the Random class and While loops in Java. The program should generate a random number from 50 through 100. The loop should add the five random numbers generated. On each iteration of the loop the program should print out the number generated, how many numbers have been generated thus far, and the sum so far. On the last iteration, the printout should say the The final total is.... Teach comment the last time I did it:...
Look for some finshing touches java help with this program. I just two more things added to this code. A loop at the end that will ask the user if they want to quit if they do want to quit the program stops if they don't it loads a new number sequence. import java.util.Random; import java.util.ArrayList; import java.util.Scanner; import java.util.Arrays; import java.util.List; import java.util.Collections; public class main { public static void main(String[] args) { List < Sequence > list =...
package week_4; import input.InputUtils; import java.lang.reflect.Array; import java.util.ArrayList; import java.util.Random; import static input.InputUtils.positiveIntInput; import static input.InputUtils.yesNoInput; /** Write a program to roll a set of dice. Generate a random number between 1 and 6 for each dice to be rolled, and save the values in an ArrayList. Display the total of all the dice rolled. In some games, rolling the same number on all dice has a special meaning. In your program, check if all dice have the same value,...