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 7666019
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T14:43:41+00:00 2026-05-31T14:43:41+00:00

How can I proceed in a controller based on whether just one part of

  • 0

How can I proceed in a controller based on whether just one part of a complex model has produced the correct flag?

A controller class is playing a queue of Midi sequences while holding onto an instance of a model class that is dynamically updated via user button presses. After the Midi queue ends, the controller needs to synchronize with the model to check that the user has made a certain number of entries before proceeding to update the interface and move to the next part of the application. The Model represents quite a lot of other data in addition to the ArrayList of user button presses, so the challenge is how to best compartmentalize the synchronization part.

Right now, the pattern I’m trying is something like the following, which doesn’t work because of nested class access between the controller and the model:

//Controller
...
Thread entriesCoordination = new Thread( new Model.InnerClass);
entriesCoordination.start();

Thread t = new Thread (this);
t.run();

...
//in runnable nested class in controller
private Model.InncerClass c = new Model.InnerClass();
public void run() {
    synchronized( c) { 
        while (!c.hasFinishedEntries()){
            try{
                c.wait();
            } catch (InterruptedException ignore{}
            }
        }
    }
    //Midiqueue completed and Entries finished
}
//in Model
//in runnable nested class in Model
public synchronized boolean hasFinishedEntries() {
    return fIsFinishedWithEntries;
}

public void run() {
    while(true) {
        try{
            synchronized(this) {
                try{
                    if(entriesArray.size() == max_size) {
                        fIsFinishedWithEntries = true;
                        notifyAll();
                    } else {...}
                }
            }
        }
    }
}

Furthermore, this seems wasteful because it basically means that I need to create a thread and run the inner class of the Model in parallel the entire duration of the time that the user can make these button selections, rather than something that would just poll the Model when I know that the Midi queue has ended.

What’s the design pattern to synchronize to one flag in a Model class from a Controller class without having to make a inner class in the model just to handle the synchronization.

  • 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-31T14:43:42+00:00Added an answer on May 31, 2026 at 2:43 pm

    I think the right thing to do here is to use an AtomicBoolean and define methods on each of your thread objects to get and set the boolean.

    The Model.InnerClass would be changed to add the AtomicBoolean and to change the getter to not be synchronized.

    private final AtomicBoolean fIsFinishedWithEntries = new AtomicBoolean();
    public boolean hasFinishedEntries() {
        return fIsFinishedWithEntries.get();
    }
    

    In the run method it something needs to set the finished boolean to be true.

    public void run() {
        while(true) {
                if (entriesArray.size() == max_size) {
                    synchronized (this) {
                        fIsFinishedWithEntries.set(true);
                        notifyAll();
                    }
                } else {...}
            }
        }
    }
    

    You’ll need to rest it to false somewhere if you are doing this more than once.

    Right now, the pattern I’m trying is something like the following, which doesn’t work because of nested class access between the controller and the model:

    You need to first create your Model.InnerClass instance and inject that into your controller thread. Making the hasFinishedEntries() be static is ugly so instead in your controller you’d call:

    private Model.InnerClass innerClass;
    public ControllerThread(Model.InnerClass innerClass) {
        this.innerClass = innerClass;
    }
    ...
    public void run() {
        synchronized (innerClass) {
            while (!innerClass.hasFinishedEntries()){
                innerClass.wait();
            }
        }
    }
    

    How can I access whether the entries are finished without synchronizing on the entire Model class?

    You can obviously just poll the hasFinishedEntries() whenever you want to see if the queue has ended. I’m not sure of a better way to do this without a thread. Is there some way to setup a UI event which checks for a condition every so often?

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

Sidebar

Related Questions

Can a LINQ enabled app run on a machine that only has the .NET
I need some advice on how I can proceed with this issue. Using PHP
I am working on a scientific application that has readily separable parts that can
The default route in MVC {controller}/{action}/{id} is for the most part quite helpful as
I have the following entity model: public class Project { [Key] public int ProjectID
How can I do a sed regex swap on all text that preceed a
Can somebody point me to a resource that explains how to go about having
Can anyone (maybe an XSL-fan?) help me find any advantages with handling presentation of
Can you cast a List<int> to List<string> somehow? I know I could loop through
can you recommend some good ASP.NET tutorials or a good book? Should I jump

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.