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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T05:55:52+00:00 2026-06-05T05:55:52+00:00

some thread perform series of operation in its run method. When station closed it

  • 0

some thread perform series of operation in its run method. When station closed it inform all its passengers through onStationClosed. When it happens thread must to do some action(leaveStation for example) and thread must terminate without finishing all remains operations.

What is the correct way to do it:

// 1 - By checking station's state between each operationN?

public class Passenger extends Thread
{
     Station station;
     public void onStationClosed()
     {
          // Do some action before thread terminates
     }
     @Override
     public void run()
     {
         operation1();
         if(station.getState == Station.Closed) return;
         operation2();
         if(station.getState == Station.Closed) return;
         operation3();
         if(station.getState == Station.Closed) return;
         ..
         operationN();
     }
}

// 2 - Throw StationClosedException from onStationClosed and catch it in Station.

public class Passenger extends Thread
{
    Station station;
    public void onStationClosed()
    {
         // Do some action before thread terminates
         throw new StationClosedException();
    }
    @Override
    public void run()
    {
        operation1();
        operation2();
        ..
        operationN();
    }
}
  • 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-05T05:55:53+00:00Added an answer on June 5, 2026 at 5:55 am

    The first solution is quite good. However not very dry, consider wrapping operations in some small action objects and check the station status before executing each operation:

    List<Operation> operations = Arrays.asList(new Operation1(), new Operation2(), new Operation3());
    
    for(Operation operation: operations) {
      if(!perform(operation)) {
        return;
      }
    }
    

    Where perform() is defined as follows:

    private boolean perform(Operation operation) {
      if(station.getState == Station.Closed)
        return false;
      operation.run();
      return true;
    }
    

    A little bit far fetched, but when the number of operations grow, you’ll appreciate it.

    I don’t quite understand the exception solution. If you throw that exception from onStationClosed() callback it will be thrown back to your event sender thread, not Passenger thread. It won’t interrupt your thread.

    However you can control this flow using InterruptedException. This solution is very similar to checking station status, but instead you check Thread.isInterrupted() flag. Added benefit: I/O operations and sleeps are automatically interrupted. All you have to do is calling

    Thread passenger = new Passenger();
    passenger.interrupt();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

My understanding is that if I start up another thread to perform some actions,
Hi all i have a background worker thread and some unmanaged code dlls ,
I am writing a Windows Form application. It has a thread to perform some
I perform some decoding process in the native code and call a Java method
How can I destroy such a thread that perform very long operation? I need
I'm planning on using the SynchronizationContext class to perform some cross-thread marshalling of UI
I am trying to perform some work in a separate thread and stop that
I have a Swing application that uses a Java Thread to constantly perform some
When i start some thread in my program, everything else is stopped. This is
I have some Thread count pCount and I have some float[] array. I want

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.