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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T03:24:38+00:00 2026-06-04T03:24:38+00:00

I was working with java.net.Authenticator to create a blocking dialog that requests proxy login/password

  • 0

I was working with java.net.Authenticator to create a blocking dialog that requests proxy login/password from the user first time any connection is established through proxy. Authenticator works just fine, but i got into some strange problem when i tried to synchronize the method that displays the input dialog.

Here is an abstract working code example of the problem i found:

private static JFrame frame;

public static void main ( String[] args )
{
    frame = new JFrame ( "Frame" );
    frame.add ( new JLabel ( "This is main application" ) );
    frame.setSize ( 500, 500 );
    frame.setLocationRelativeTo ( null );
    frame.setDefaultCloseOperation ( JFrame.EXIT_ON_CLOSE );
    frame.setVisible ( true );

    // Cycling thread
    new Thread ( new Runnable ()
    {
        public void run ()
        {
            while ( true )
            {
                // Opening new dialog in a separate event dispatch thread
                SwingUtilities.invokeLater ( new Runnable ()
                {
                    public void run ()
                    {
                        showSomeLockingDialog ();
                    }
                } );

                // Wait 3 seconds before next window
                try
                {
                    Thread.sleep ( 3000 );
                }
                catch ( InterruptedException e )
                {
                    e.printStackTrace ();
                }
            }
        }
    } ).start ();
}

private static final Object lock = new Object ();

private static void showSomeLockingDialog ()
{
    synchronized ( lock )
    {
        // Output to see that this method is not finished yet
        System.out.println ( "Method start" );

        // Modal thread-blocking dialog
        JDialog dialog = new JDialog ( frame, "Lock" );
        dialog.add ( new JLabel ( "This should be blocking other dialogs" ) );
        dialog.pack ();
        dialog.setLocationRelativeTo ( null );
        dialog.setModal ( true );
        dialog.setVisible ( true );

        // Output to see that this method is not finished yet
        System.out.println ( "Method end" );
    }
}

So basically:

  1. There is a visible main frame (actually, just to keep JVM running)
  2. There is cycling Thread that requests showSomeLockingDialog() method in each cycle
  3. Method is synchronized on the “lock” object
  4. There is console output at the method start and end

So if you run this example you will see that each cycle a new dialog pops ignoring synchronization even if you do not close previous dialog. I have also tried simple method synchronization instead but it has the same effect.

Everything changes if we change the way we call showSomeLockingDialog() a bit:

    new Thread ( new Runnable ()
    {
        public void run ()
        {
            showSomeLockingDialog ();
        }
    } ).start ();

(just using a separate thread instead of calling method inside event dispatch thread)

This way everything works the way i expect it to work – new dialog calls are blocked until the one that was called before is closed.

And that is pretty strange – what is so special about event dispatch thread that synchronization is getting ignored?

Or if it is actually a bug – is there any workaround?
Maybe i am missing something huge…

Some thoughts: It seems to me that modal dialog setVisible method acts differently inside event dispatch thread (otherwise it would block the whole interface if called from there). But how does that affect synchronization…

P.S. And no, i can’t just use 2nd (working) example in my specific case, because i am not calling that method where i want – it is getting called from random place, mostly from standart JDK classes (when any resource that is getting loaded from internet – either image in JLabel, some URL input stream or anything else).

  • 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-04T03:24:39+00:00Added an answer on June 4, 2026 at 3:24 am

    From the Dialog’s setVisible javadoc documentation:

    It is OK to call this method from the event dispatching thread because 
    the toolkit ensures that other events are not blocked while this method
    is blocked.
    

    And based on that java synchronized blocks are reentrant, the following is what is happening for each invokeLater:

    1. The dispatch thread is calling showSomeLockingDialog
    2. taking the lock or reentering
    3. Open window -> blocked until closing the dialog

    Since blocking is just for the setVisible and not for other events (ie. other invokeLaters) then you get the specified behavior.

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

Sidebar

Related Questions

Im working on an application and was trying things from http://today.java.net/pub/a/today/2007/11/13/mapping-mashups-with-jxmapviewer.html but while trying
I'm a bit new to .Net development, been working in Java for some time
I am working on porting an existing VB.Net application to Java, and could not
When working with Java from Scala, we have to account for null. HttpServletRequest getters
I am working on a java program that is essentially a chat room. This
I have a working java client that is communicating with Google, through ProtoBuf serialized
I have a java project that I'm working on which was working until a
I have a working prototype of a Java application that is using Apache Commons
While working on a larger project, a few lines of code using java.net.Socket failed
http://www.hccp.org/java-net-cookie-how-to.html According to this link I was trying to create cookie and send cookie

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.