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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T11:16:22+00:00 2026-05-12T11:16:22+00:00

My strategy for threading issues in a Swing Java app is to divide methods

  • 0

My strategy for threading issues in a Swing Java app is to divide methods in three types:

  1. methods that should be accessed by the GUI thread. These methods should never block and may call swing methods. Not thread-safe.
  2. methods that should be accessed by non-GUI threads. Basically this goes for all (potentially) blocking operations such as disk, database and network access. They should never call swing methods. Not thread-safe.
  3. methods that could be accessed by both. These methods have to be thread-safe (e.g. synchronized)

I think this is a valid approach for GUI apps, where there are usually only two threads. Cutting up the problem really helps to reduce the “surface area” for race conditions. The caveat of course is that you never accidentally call a method from the wrong thread.

My question is about testing:

Are there testing tools that can help me check that a method is called from the right thread? I know about SwingUtilities.isEventDispatchThread(), but I’m really looking for something using Java annotations or aspect-oriented programming so that I don’t have to insert the same boilerplate code in each and every method of the program.

  • 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-12T11:16:23+00:00Added an answer on May 12, 2026 at 11:16 am

    Thanks for all the tips, here is the solution I came up with in the end. It was easier than I thought. This solution uses both AspectJ and Annotations. It works like this: simply add one of the annotations (defined below) to a method or a class, and a simple check for EDT rule violations will be inserted into it at the beginning. Especially if you mark whole classes like this, you can do a whole lot of testing with only a tiny amount of extra code.

    First I downloaded AspectJ and added it to my project (In eclipse you can use AJDT)

    Then I defined two new Annotations:

    import java.lang.annotation.ElementType;
    import java.lang.annotation.Target;
    
    /**
     * Indicates that this class or method should only be accessed by threads
     * other than the Event Dispatch Thread
     * <p>
     * Add this annotation to methods that perform potentially blocking operations,
     * such as disk, network or database access. 
     */
    @Target({ElementType.METHOD, ElementType.TYPE, ElementType.CONSTRUCTOR})
    public @interface WorkerThreadOnly {}
    

    and

    import java.lang.annotation.ElementType;
    import java.lang.annotation.Target;
    
    /**
     * Indicates that this class or method should only be accessed by the 
     * Event Dispatch Thread
     * <p>
     * Add this annotation to methods that call (swing) GUI methods
     */
    @Target({ElementType.METHOD, ElementType.TYPE, ElementType.CONSTRUCTOR})
    public @interface EventDispatchThreadOnly {}
    

    After that, I defined the Aspect that does the actual checking:

    import javax.swing.SwingUtilities;
    
    /** Check methods / classes marked as WorkerThreadOnly or EventDispatchThreadOnly */
    public aspect ThreadChecking {
    
        /** you can adjust selection to a subset of methods / classes */
        pointcut selection() : execution (* *(..));
    
        pointcut edt() : selection() && 
            (within (@EventDispatchThreadOnly *) ||
            @annotation(EventDispatchThreadOnly));
    
        pointcut worker() : selection() && 
            (within (@WorkerThreadOnly *) ||
            @annotation(WorkerThreadOnly));
    
        before(): edt() {
            assert (SwingUtilities.isEventDispatchThread());
        }
    
        before(): worker() {
            assert (!SwingUtilities.isEventDispatchThread());
        }
    }
    

    Now add @EventDispatchThreadOnly or @WorkerThreadOnly to the methods or classes that should be thread-confined. Don’t add anything to thread safe methods.

    Finally, Simply run with assertions enabled (JVM option -ea) and you’ll find out soon enough where the violations are, if any.

    For reference purposes, here is the solution of Alexander Potochkin, which Mark referred to. It’s a similar approach, but it checks calls to swing methods from your app, instead of calls within your app. Both approaches are complimentary and can be used together.

    import javax.swing.*;
    
    aspect EdtRuleChecker {
        private boolean isStressChecking = true;
    
        public pointcut anySwingMethods(JComponent c):
             target(c) && call(* *(..));
    
        public pointcut threadSafeMethods():         
             call(* repaint(..)) || 
             call(* revalidate()) ||
             call(* invalidate()) ||
             call(* getListeners(..)) ||
             call(* add*Listener(..)) ||
             call(* remove*Listener(..));
    
        //calls of any JComponent method, including subclasses
        before(JComponent c): anySwingMethods(c) && 
                              !threadSafeMethods() &&
                              !within(EdtRuleChecker) {
         if(!SwingUtilities.isEventDispatchThread() &&
             (isStressChecking || c.isShowing())) 
         {
                 System.err.println(thisJoinPoint.getSourceLocation());
                 System.err.println(thisJoinPoint.getSignature());
                 System.err.println();
          }
        }
    
        //calls of any JComponent constructor, including subclasses
        before(): call(JComponent+.new(..)) {
          if (isStressChecking && !SwingUtilities.isEventDispatchThread()) {
              System.err.println(thisJoinPoint.getSourceLocation());
              System.err.println(thisJoinPoint.getSignature() +
                                    " *constructor*");
              System.err.println();
          }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 204k
  • Answers 204k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer There are a few reasons to use static in C.… May 12, 2026 at 8:47 pm
  • Editorial Team
    Editorial Team added an answer Why would you expect Python to provide an "elegant way"… May 12, 2026 at 8:47 pm
  • Editorial Team
    Editorial Team added an answer It would mostly depend on how many tasks you have… May 12, 2026 at 8:47 pm

Related Questions

I'm building a site with django that lets users move content around between a
I have a database class that automatically sets up a connection to the database
I'm writing a little desktop app that should be able to encrypt a data
Below is my (simplified) schema (in MySQL ver. 5.0.51b) and my strategy for updating

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.