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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T11:11:37+00:00 2026-06-10T11:11:37+00:00

I came across this interesting situation while answering this question . Try this piece

  • 0

I came across this interesting situation while answering this question.

Try this piece of poorly designed code –

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

class abc extends JFrame implements ActionListener
{

boolean button_clicked = false;
JButton b1;

abc(){
    this.setSize (400, 400);
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    this.createUI();
}

void createUI(){
    this.setLayout(null);
    b1 = new JButton("Click here");
    b1.setSize(110,30);
    b1.setLocation(10,210);
    this.add(b1);
    b1.addActionListener(this);
}

public boolean isButton_clicked()
{
    return button_clicked;
}

public void setButton_clicked(boolean button_clicked) {
    this.button_clicked = button_clicked;
}



public void actionPerformed(ActionEvent arg0) {
    button_clicked = true;
}

}

Here’s the main method.

class tempMain extends JFrame
{

public static void main(String[] args) throws Exception
{
    abc temp = new abc();
    temp.setVisible(true);
    while(true)
    {
    //  Thread.sleep(200);
        if(temp.isButton_clicked())
        {
            JOptionPane.showMessageDialog(null, "Hello");
            temp.setButton_clicked(false);
        }
    }
}
}

When I ran this on my Windows 7 machine, nothing happened for at least about a minute (I didn’t wait after that) after I clicked the button.

Now, just make one small change –

  Thread.sleep(200); // uncomment this from the main.

And surprisingly, it works and a JOptionPane message is displayed. Why is the message not displayed the first time?

  • 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-06-10T11:11:38+00:00Added an answer on June 10, 2026 at 11:11 am

    And surprisingly, it works and a JOptionPane message is displayed. Why is the message not displayed the first time?

    button_clicked is not marked as volatile and is being updated from a different thread than the main thread. Since the call-back is made from the event handler thread, the main thread will not see the update unless button_clicked is defined as a volatile boolean. Putting a sleep may allow a memory barrier to be crossed and button_clicked to be updated in the main thread luckily.

    Here’s some more reading about volatile and why it is important when we are dealing with multiple threads.


    Another issue is that you have an infinite loop which is spewing messages to System.out. This completely blocks after a while because the console can’t display lines that fast which stops the checking for the click.

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

Sidebar

Related Questions

While answering this question I came across an interesting difference in the overload resolution
I have a question I came across an interesting piece of code. In MySQL
So I came across this interesting problem while reviewing code: class Foo: def __init__(self,
I came across this situation while migrating our DB from Foxpro to SQL. Below
While searching I came across this . It looks interesting but I have absolutely
While browsing the code of an erlang application, I came across an interesting design
I came across some interesting code in java.lang.Class#newInstance0: // Run constructor try { return
I came across an interesting situation when using this class: class Company(models.Model): date =
Came across this code: <?php require_once 'HTTP/Session/Container/DB.php'; $s = new HTTP_Session_Container_DB('mysql://user:password@localhost/db'); ini_get('session.auto_start') or session_start();
I came across this Linq to Sql code in an application I am maintaining:

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.