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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T23:55:16+00:00 2026-06-06T23:55:16+00:00

I am building a multi-threaded project in Java, I have entities and DAO packages

  • 0

I am building a multi-threaded project in Java, I have entities and DAO packages to wrap the database tables and manipulating them. I have processing package which contains Runnables. The way i have implemented the Runnables so far is like this:

Class Thread1 implements Runnable{
  Thread t;
   parameters
  ...

  public Thread1(){
    t = new Thread(this,"Thread1");
    ....
    t.start();
  }

  public int method1(){
  ...
  return x;
  }

  public double method2(){
  ...
  return y;
  }

  public void run(){
    // some processing using DAO methods
    ....
    method1();
    ... 
    method2();
    ...
  }

}

The code works this way, but i need to use the same processing in the run() method as part of the processing in Thread2 class. The way i have structured my code does not allow to reuse the code. what would be a better structure to resolve this?

  • 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-06T23:55:18+00:00Added an answer on June 6, 2026 at 11:55 pm

    You could either:

    1. Make Thread1 and Thread2 extend the same abstract base class, and move shared logic to the parent (inheritance).
    2. Create a common interface that both classes implement and that contains the common methods, then create a separate class that implements Runnable, and implement run() there (composition).

    You should always favour composition over inheritance, so the second option is usually better, because it also gives you the flexibility of changing behaviour at runtime.

    For example:
    Create a shared interface first

    public interface SharedTask {
        public void method1();
        public void method2();
    }
    

    Make both classes implement it:
    public class Thread1 implements SharedTask and public class Thread2 implements SharedTask.

    public class Worker implements Runnable {
    
        public Worker (SharedTask task) {
            this.task = task;
            ...
        }
    
        public void run() {
            task.method1();
            task.method2();
        }
    }
    

    Elsewhere in your code:
    new Worker().start();

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

Sidebar

Related Questions

I'm building a multi-threaded windows service application in Delphi XE2 which uses ADO database
I'm building a multi-threaded client side javascript application, and I would like to have
I have two database tables (lets call them A and B) with many-to-many bridge
I have been building a multi threaded server, with each thread having a single
I have a problem with building multi level select box. I have category table
I have a specific question, that could use a general answer... When building Multi-Tier
We are building a multi-tenant server with a database/schema per tenant. To do this
I am building a multi threading application. I have setup a threadPool. [ A
We're building a multi-language Drupal stack and one of the concerns we have is
I am building up a multi-threaded application where I spawn three threads when application

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.