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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T19:37:55+00:00 2026-06-02T19:37:55+00:00

suppose we have these classes and read the comments class Work { void doWork(){

  • 0

suppose we have these classes and read the comments

class Work {
void doWork(){  }
void commit(){}       
}
class MyRunable implements Runnable {
run(){
   Work  work=new Work();
   work.doWork();
//i can't write work.commit() here, because sometimes i want Thread runs both methods       
 //and sometimes runs only doWork()
 }
}
class Tasks{
main(){
MyRunable myRunable=new MyRunable();
Thread t=new Thread(myRunable);
t.start();
//suppose now i need to call commit() method by the same thread (t)
//how can i do that 
}
}

also i don’t want to use constructor to determine if i want to call both method or not

  • 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-02T19:37:56+00:00Added an answer on June 2, 2026 at 7:37 pm

    You could try using a thread pool with a single thread and keep enqueuing methods as needed:

    class Tasks {
        public static void main(String[] args) {            
    
            ExecutorService exec = Executors.newSingleThreadExecutor();
            final Work work = new Work();
            exec.submit(new Runnable() {
                    public void run() {
                        work.doWork();
                    }
                });
            // later
            exec.submit(new Runnable() {
                    public void run() {
                        work.commit();
                    }
                });
    
        }
    }
    

    This way, both methods will be executed in a sequence by the same thread, but separately.

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

Sidebar

Related Questions

Let's suppose I have these classes: class A(): msg = 'hehehe' class B(A): msg
Suppose we have declared these two classes: public class Animal { //..... } public
suppose we have these classes: public class Record { int key; int value; Record(){
Suppose I have several services represented by classes ServiceA, ServiceB, ServiceC etc. These services
I'm using Hibernate JPA. Suppose I have these classes: AbstractPerson |--> ConcreteEmployee |--> ConcreteCustomer
Suppose I have these interfaces: public interface I1 { void foo(); } public interface
Suppose we have an abstract class Element from which classes Triangle and Quadrilateral are
Suppose I have a definition for a door: class Door { public void Lock()
Suppose I have an event KeyPress subscribed to by various classes. Assume that Class
I have 2 classes: class Base { public: virtual int Foo(int n); virtual void

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.