Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6598257
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T18:19:16+00:00 2026-05-25T18:19:16+00:00

This may be a really simple question, but things I’ve tried don’t seem to

  • 0

This may be a really simple question, but things I’ve tried don’t seem to work. I admit my understanding of Java is not that great and this is the first time I’ve worked with Threads, so here it goes,

public MockDataGenerator() {

    new Thread() {
        @Override
        public void run() {
            while (true) {
                try {
                    if (bayeuxIds.size() > 0) {

                        System.out.println("These are the bayeuxIds from new Thread" + bayeuxIds);
                        List<String> users = new ArrayList<String>();
                        users.addAll(bayeuxIds);
                        setData(users);
                        Collections.shuffle(users);
                        String bayeuxId = users.get(0);
                        List<Alert> alerts = generateRandomAlerts(1);
                        alertsAdded(bayeuxId, alerts);

                    }
                } catch (Throwable t) {

                } finally {
                    try {
                        int numUsers = bayeuxIds.size();

                        // plus 1 prevents divide by zero
                        sleep(30000);
                        //sleep(r.nextInt(120000 / (numUsers + 1)));

                    } catch (InterruptedException e) {
                    }
                }
            }
        }
    }.start();

}

public void onMessage(Message message) {
    System.out.println("Message recieved from ActiveMQ");


    if (message instanceof TextMessage) {
        try {

            System.out.println("This is the 0th element of usersCopy: " + users.get(0));
            String bayeuxId = users.get(0);


            String theMsg = ((TextMessage) message).getText();
            clientMessanger.sendUpdate(theMsg, bayeuxId);

        } catch (Exception e) {
            e.printStackTrace();
        }
    } else {
        throw new IllegalArgumentException("Message must be of type TextMessage");
    }

}

Edit 1:

Edited to show the full code, basically what I want is the users List from the Thread to be passed into the onMessage function. I tried some getters and setters but that did not work since I assume I have to use a syncronized function in order for that to work right?

Edit 2:

Added these functions but it still doesn’t seem to be setting the variables right. What am I doing wrong?

    String bayeuxIdCopy;

public synchronized void setData(String bayeuxId) {
    System.out.println("This is the bayeuxID being set:  " + bayeuxId);
    bayeuxIdCopy = bayeuxId;
}

public synchronized String getData() {
    System.out.println("This is the bayeuxID being returned:  " + bayeuxIdCopy);
    return bayeuxIdCopy;
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T18:19:18+00:00Added an answer on May 25, 2026 at 6:19 pm

    You may want to extend the Thread class and create a constructor that takes an object that you instantiate before hand and pass the object in to the constructor. This will allow the Thread to alter the object and be able to see the changes outside the thread. This is the cleanest way.

    Also you can create an object inside the MockDataGenerator and outside the new Thread statement. This will also be accessible outside the thread with the changes.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This one may seem like a simple question, but it's really got me scratching
Ok, this may seem like a stupid question (for Flash Developers) but I really
This may be a really simple question, but I'm trying to create the database
This may seem like a daft question, but i was wondering about how to
I know this question seems subjective but it's really pretty simple. As a long
Ok, this may seem like a dupe question, but if you bear with me
This may not really sound hard to do but it is currently killing me.
This may seem like a programming 101 question and I had thought I knew
This may be simple one, but 5 mins of Googling didn't give me the
This may be a simple fix - but I'm trying to sum together all

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.