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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T18:02:13+00:00 2026-05-30T18:02:13+00:00

I need to have two classes, one class has two methods each of which

  • 0

I need to have two classes, one class has two methods each of which will take a while to execute completely. The second class is given information which will decide which one of the two methods in the first class will execute. It is important however that if the second class (which will be executing at the same time in its own thread) decides that the other of the two methods should execute, the first class would go straight to executing the other method and not have to wait for the first to finish.

If class A has the two methods and class B is in parallel in a different thread deciding which method to execute then here is an example:
A is executing method 1 which will take a while to return. During the execution of method 1, class B decides method 2 should execute which means class A should immediately stop doing method 1 and go on to execute method 2.

Is this possible? Maybe with interrupts?

thanks

  • 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-30T18:02:15+00:00Added an answer on May 30, 2026 at 6:02 pm

    Here’s an outline of something that should work:

    class ClassA implements Runnable {
    
        public volatile boolean switchToOther;
    
        @Override
        public void run() {
            switchToOther = false;
            try {
                method1();
            } catch (InterruptedException e) {
                // Restore the interrupted status
                Thread.currentThread().interrupt();
            }
            if (switchToOther) {
                method2();
            }
        }
    
        public void method1() throws InterruptedException {
            work();
        }
    
        public void method2() throws InterruptedException {
            otherWork();
        }
    }
    
    class ClassB implements Runnable {
    
        @Override
        public void run() {
            ClassA a = new ClassA();
            Thread t = new Thread(a);
            t.start();
            if (decideToSwitch()) {
                a.switchToOther = true;
                t.interrupt();
            }
        }
    }
    

    Note:

    1. InterruptedException can happen beyond your control, so you should check it’s due to an explicit switch,
    2. the volatile on the switchToOther field used from several threads,
    3. the resetting of the interrupted state, which is standard idiom when catching InterruptedException
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two classes that each need an instance of each other to function.
I have two classes, Car and Person. Car has as one of its members
So I have one main file, and two classes: A display Class, and a
I have two classes, one is the parent of the other. They both need
I have two classes. One is called Employee and the other EmployeeDetails that has
Say I have two classes One, the Foo class, interfaces with the user, and
I have two classes, one has a lot of properties and the other has
I need to have two UITableViews on one UIView. I can make it work
I have two unsorted lists and I need to produce another list which is
I have two application that need to talk to each other. App1 needs to

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.