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

  • SEARCH
  • Home
  • 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 7006923
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T21:32:23+00:00 2026-05-27T21:32:23+00:00

Each time a back-end message comes I add it to JList and JList is

  • 0

Each time a back-end message comes I add it to JList and JList is being refreshed using fireIntervalAdded. The problem is that in one second 20 messages may arrive and each one of them will call fireIntervalAdded. What I would like to do is to stack all messages in List and send one big stack of data to JList. My current solution does not seem to work, it always sends one message instead of one big stack:

    private class StackingListener implements MessageListener {
    private List<Message> messages = new LinkedList<Message> ();
    private int waiting = 0;
    @Override
    public void messageReceived(MessageEvent event) {
        stackData(event.getData());
    }

    private void stackData(Message data) {
        messages.add(data);
        if (waiting <= 0) {
            waiting = 3;
            new Thread(new Runnable() {
                @Override
                public void run() {
                    while(--waiting > 0) {
                        try {
                            Thread.sleep(500);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                    List<Message> list = new ArrayList<Message>(messages);
                    messages.clear();
                    logger.info("Adding list with size of " + list.size());
                    controller.getListModel().addFullElements(list);
                }
            }).run();
        } else {
            waiting = 3;
        }
    }
}

I think I’m doing something very wrong. The idea of this code is to stack up messages while Thread is sleeping. But seems like Thread.sleep stops everything, not only the current thread.

Thanks

  • 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-27T21:32:24+00:00Added an answer on May 27, 2026 at 9:32 pm

    You are using Thread.run() which just calls the run() method in the current thread. What you intended to use was Thread.start() creates a thread and call run() in that new thread.

    However I wouldn’t structure the code like this at all. I believe a simpler apporach is to use a queue.

    class StackingListener implements MessageListener, Runnable {
        private final BlockingQueue<Message> messages = new LinkedBlockingDeque<Message>();
        private final ScheduledExecutorService service = Executors.newSingleThreadScheduledExecutor(); {
            service.scheduleAtFixedRate(this, 500, 500, TimeUnit.MILLISECONDS);
        }
    
        @Override
        public void messageReceived(MessageEvent event) {
            messages.add(event.getData());
        }
    
        @Override
        public void run() {
            final List<Message> list = new ArrayList<Message>();
            messages.drainTo(list);
    
            logger.info("Adding list with size of " + list.size());
            // add to the GUI component in a thread safe manner.
            SwingUtilities.invokeLater(new Runnable() {
                @Override
                public void run() {
                    controller.getListModel().addFullElements(list);
                }
            });
        }
    
        public void stop() {
            service.shutdown();
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Each time a python file is imported that contains a large quantity of static
I'm getting this message each time I compile my project: RunIPhoneUnitTest.sh: line 92: 31389
The webViewDidFinishLoad message seems to be sent each time any object in the page
I set up a hitcounter so that each time someone goes to my site,
I need to insert onto the end of a vector, 2 elements each time.
I have a java back-end that needs to expose services to clients running in
I am developing the back end of a CMS that I have been working
Our application (using a SQL Server 2008 R2 back-end) stores data about remote hardware
We have a front end written in Visual Basic 6.0 that calls several back
Each time my webpage is loaded it runs a routine in the page_load event

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.