Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Java mutex causing "missed input" compared to busy loop?

  1. #1
    Junior Member
    Join Date
    Apr 2021
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Java mutex causing "missed input" compared to busy loop?

    Hey everyone, I'm writing a Java program that interfaces with a C program on my Linux server (it's a client/server chat program). Right now, I'm implementing a blocking feature for the input to block until the user presses "Enter" before sending the input to the server. To do this, I have two choices: a busy loop, and a mutex. The mutex is obviously the best alternative, but I'm running into an issue where sometimes the input just won't send to the server at *all*. With the busy loop, though, I just unset a flag and it works fine.

    Busy loop:

    String line = "";
    // Block until we receive some input.
    while (!this.inputField.isReady()) {
    }
    line = this.inputField.getText();
    this.writefp.write(line + "\n");
    this.writefp.flush();
    this.inputField.setReady(false);

    So, all I need to do in the textfield (in a keylistener) is:
    ...
    public void keyPressed(KeyEvent e) {
      if (e.getKeyCode() == KeyEvent.VK_ENTER) {
        ready = true;
        setText("");
      }
    }
    ...

    So, yes, this works. My mutex solution isn't so lucky:
    while (!this.inputField.isReady()) {
      synchronized(this.inputField.mutex) {
        try {
          this.inputField.mutex.wait();
        } ... //redacted for simplicity
      }
    }
    And the notifier:
    ...
    ready = true; 
    // This is in the TextField class.
    synchronized(this.mutex) {
      this.mutex.notify();
    }
    setText("");
    ...
    Maybe I'm misunderstanding a fundamental piece of Java's synchronization capabilities (this is much easier in C...). the mutex is just a standard Object, following online examples. Any help would be greatly appreciated.

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    24,109
    Thanks
    53
    Thanked 2,618 Times in 2,568 Posts

    Default Re: Java mutex causing "missed input" compared to busy loop?

    block until the user presses "Enter" before sending the input
    That sounds like the normal way a GUI program works. It doesn't do anything until it receives an event like the user pressing a button.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Replies: 1
    Last Post: August 20th, 2019, 07:07 AM
  2. Replies: 6
    Last Post: August 16th, 2014, 01:34 AM
  3. Replies: 2
    Last Post: May 22nd, 2014, 01:17 PM
  4. Replies: 2
    Last Post: June 22nd, 2013, 10:30 AM
  5. Replies: 1
    Last Post: January 15th, 2010, 01:32 AM

Tags for this Thread