Question

In: Computer Science

I'm trying to Generate number every 3 seconds and update the currenet number.I'm able to generate...

I'm trying to Generate number every 3 seconds and update the currenet number.I'm able to generate number every 3 seconds; However, the current number isn't update. I apperciate any help.

public static void main(String[] args) {
    
    Runnable helloRunnable = new Runnable() {
        public void run() {
      
            CurrentNum=task2();
        
            System.out.println("Result    ====    "+CurrentNum);

        }
    };

    ScheduledExecutorService executor = Executors.newScheduledThreadPool(1);
    executor.scheduleAtFixedRate(helloRunnable, 0, 3, TimeUnit.SECONDS);
    
     System.out.println("CurrentNum    ====    "+CurrentNum);
     
    
}
public static int task2() {
    // create instance of Random class 
    Random rand = new Random(); 

    // Generate random integers in range 0 to 999 
    int rand_int1 = rand.nextInt(1000); 
    
    return rand_int1;
}

outPut:

    CurrentNum    ====    0
    Result    ====    631
    Result    ====    789
    Result    ====    958

I want the output tobe:

    Result    ====    631
    CurrentNum ====    631
    Result    ====    789
    CurrentNum====    789

Solutions

Expert Solution

CODE:

import java.util.*;
import java.util.concurrent.*;
class Generate
{
static int CurrentNum;//we have declare this as static in order change it in innerclass.
public static void main(String[] args)
{
  
Runnable helloRunnable = new Runnable()
{
  
public void run() {
  
CurrentNum=task2();

System.out.println("Result ==== "+CurrentNum);
}
};
//here the value is changed but not viewed because we cannot concurrent main method.
while(true)
{
//here the executor executes only the runnable method not the main method.
ScheduledExecutorService executor = Executors.newScheduledThreadPool(1);
executor.scheduleAtFixedRate(helloRunnable, 0, 3, TimeUnit.SECONDS);
System.out.println("CurrentNum ==== "+CurrentNum);
}
}
public static int task2() {
// create instance of Random class
Random rand = new Random();

// Generate random integers in range 0 to 999
int rand_int1 = rand.nextInt(1000);
  
return rand_int1;
}
}

CODE ATTACHMENTS:

OUTPUT:

Here in your code there is no error but the concurrent time is printed only once.

The value of concurrentnum is changing but not visualised as the scheduledexecutor only executes the runnable not the main.

When the run method and loop are synchronized then prints the required values as shown in the output.

However they both continuously execute infinitely and you can find the required output.

Please do comment for any queries.
PLease like it.
Thank You.


Related Solutions

I'm generating a random number every 10 second; In arraylist I'm Storing and Updating the number...
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 {...
I'm trying to convert between different number representations in C++ , I have the prototype but...
I'm trying to convert between different number representations in C++ , I have the prototype but im not sure what do do from here bool convert2(int & n, const string & bits); bits should have size exactly 5, and each char of bits should be '0' or '1'; otherwise return false. If bits is ok, set n to the number that bits represents as a 2's complement number and return true. For example, convertU(n, "10011") should set n to -13...
I'm trying to convert between different number representations in C++ , I have the prototype but...
I'm trying to convert between different number representations in C++ , I have the prototype but im not sure what do do from here bool convertU(unsigned & n, const string & bits); bits should have size exactly 5, and each char of bits should be '0' or '1'; otherwise return false. If bits is ok, set n to the number that bits represents as an unsigned and return true. For example, convertU(n, "0101") and convertU(n, "10210") should return false; convertU(n,...
I'm trying to convert between different number representations in C++ , I have the prototype but...
I'm trying to convert between different number representations in C++ , I have the prototype but im not sure what do do from here bool convertF(double & x, const string & bits); bits is supposed to be an 8-bit (not 5-bit) value, each char being '0' or '1'; if not, return false. The first bit of bits represents the sign bit, the next 3 bits represent the exponent, and the next 4 bits represent the significand. convertF may assume without...
I'm trying to write a program that reads 3 heights of kids and puts them in...
I'm trying to write a program that reads 3 heights of kids and puts them in ascending order using if and else statements but i'm having trouble #include<stdio.h> int main() { int k1,k2,k3; printf("Enter height of kid #1 >"); scanf("%d",&k1); printf("Enter height of kid #2 >"); scanf("%d",&k2); printf("Enter height of kid #3 >"); scanf("%d",&k3); if(k3>k2>k1) { printf("In ascending order %d %d %d",k3,k2,k1); } else if(k3>k1>k2) { printf("In ascending order %d %d %d",k3,k1,k2); } else if(k2>k3>k1) { printf("In ascending order %d...
I'm trying to write a feet to meters and centimeters program and I'm looking for a...
I'm trying to write a feet to meters and centimeters program and I'm looking for a little help so I can see how close I got. It should do the following: Write a convertToMetric class that has the following field: standard - holds a double, standard length value in feet. The class should have the following methods: Constructor - that accepts a length in feet (as a double) and stores it in the standard field. setStandard - accepts a standard...
I'm trying to space out my 3 inputs when they are displayed in the list box....
I'm trying to space out my 3 inputs when they are displayed in the list box. I've tried putting "\t" and "" in between inputs and it makes the 3rd input not visible, I've tried expanding my listbox and it still wasn't visible either. private void button2_Click(object sender, EventArgs e) { //clear any previous values listBox3.Items.Clear(); for (int index = 0; index < currentCapacity; index++) { listBox3.Items.Add(string.Format("{0, -10}{1, 10}{2, 10}", name[index],numTickets[index],costs[index].ToString("C"))); } }
I only need to update this JAVA program to be able to : 1 ) Exit...
I only need to update this JAVA program to be able to : 1 ) Exit cleanly after creating the chart 2 ) change the pie chart to a perfect circle rather than an oval 3 ) provide a separate class driver import java.awt.Color; import java.awt.Graphics; import java.awt.Graphics2D; import java.awt.Rectangle; import java.util.Scanner; import javax.swing.JComponent; import javax.swing.JFrame; // class to store the value and color mapping of the // pie segment(slices) class Segment { double value; Color color; // constructor public...
I'm working on a to-do list program in Python 2. I'm trying to delete an item...
I'm working on a to-do list program in Python 2. I'm trying to delete an item from the list and I'm not sure what I'm missing to do that. I haven't been able to get it to delete by string or by index number. Also, I'm trying to get the menu to run again after the user completes the add/delete/etc options. Do I need to put menu() menu_option = int(input("Welcome to your To-Do List. Please choose and option to continue:...
Question -Please rephrase the passage below . i'm trying to elimate plagrism I'm part of an...
Question -Please rephrase the passage below . i'm trying to elimate plagrism I'm part of an energetic entrepreneurial team aspiring to create a difference in the field of Tech platforms. We are currently working on a software platform that aims to integrate range of media dvices like laptops, mobiles, video recorders and other devices. With growing number of devices in our daily life, it's becoming increasingly complex to manage all devices separately. With this advent, we aim to eliminate the...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT