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

  • Home
  • SEARCH
  • 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 112421
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T02:32:31+00:00 2026-05-11T02:32:31+00:00

I have a DBAdmin class that connects to the database, and then some other

  • 0

I have a DBAdmin class that connects to the database, and then some other classes such as Article, Category and so on, that perform certain queries to a database.

I am using these classes in a Swing application to build a sort of small Intranet CMS application.

Now, the Category class has a bunch of static methods such as addCategory, editCategory and so on, like such:

public class Category implements Runnable {      public void run () {         //Code that will be executed in a separate thread, from the Runnable Interface     }      public static int addCategory(Category cat) {         DBAdmin db = DBAdmin.getInstance();         db.connectDB();         //rest of the code that creates sql statement and so on...     }      //Other static edit and delete methods that will be called from outside } 

P.S. The DBAdmin is a Singleton Class, and the getInstance method returns the self invoked instance.

Now, what I’m trying to do is that the database actions should be run in a separate thread from the application, and I managed to do some sample tests that are run in the run method when a Thread is started.

But the problem is that I cannot specify which method should be run in the run method when a thread is started.

In the sense that when for example the addCategory is called from the outside, the run method should invoke the addCategory method inside it.

Is there a way to maybe pass a function as a callback parameter to the run method from the addCategory method so that it will know what function should be invoked in it when a new thread is started? Or is there a completely different way to achieve what I’m trying to do?

P.S. I am ‘fairly’ new to Java at this point, especially multi-threading, so I may be missing something here.

  • 1 1 Answer
  • 1 View
  • 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. 2026-05-11T02:32:32+00:00Added an answer on May 11, 2026 at 2:32 am

    It sounds like what you want is a single thread which does all the database access for the whole program, and in that case you shouldn’t be passing things to the run method. Because when you have a single thread, all it does is call a single run method once, when it starts, and that run method just keeps executing until it returns or the thread is forcibly terminated. The way to do that sort of thing is to have a synchronized queue, say, in your DBAdmin class. Then methods like addCategory would prepare their statements and add them to this shared queue. The single database thread processes the statements in the queue one at a time, and when a statement completes, it notifies the thread that added it to the queue. In fact, Java (recent versions at least) includes a set of classes that will do exactly that for you; they’re called Executors and they can be found in the java.util.concurrent package. Specifically, look at the java.util.concurrent.Executors.newSingleThreadExecutor() method, which is what I’d use for a situation like this.

    class SQLQueryCallable implements Callable<SQLResultSet> {     private String query;     public SQLQueryCallable(String query) {         this.query = query;     }     public SQLResultSet Call() throws Exception {         // execute query         return results;     } } ExecutorService ex = Executors.newSingleThreadExecutor();  // in DBAdmin somewhere:     public SQLResultSet runQuery(String query) {         Future<SQLResultSet> f = ex.submit(new SQLQueryCallable(query));         return f.get(); // this waits for the query to complete     } 

    If I’ve been misreading your question, and you actually want each database query to execute in its own thread, then I’d suggest making a class called something like SQLQuery, which implements Runnable (or extends Thread) – actually, you could even consider using java.util.concurrent.Callable, which allows you to return a value from its call() method (which is like run()). The class constructor should take an SQL statement as a parameter, and its run method should do the work to execute that statement. Then if you’ve used Callable, you can return the result of the statement, or if you’ve used Runnable, you can store it in some commonly accessible location. That’s the simple answer to what you were asking about, how to pass data to a run() method: use the class constructor.

    class SQLQueryRunnable implements Runnable {     private String query;     public SQLQueryRunnable(String query) {         this.query = query;     }     public void run() {         // execute query     } } 

    P.S. Multithreading is a hard thing to get used to but Java is a good language to do it in (my opinion).

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

Sidebar

Ask A Question

Stats

  • Questions 138k
  • Answers 138k
  • 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 What version of IE? Try adding the following CSS rule:… May 12, 2026 at 7:37 am
  • Editorial Team
    Editorial Team added an answer You've probably figured this out by now. But yes, jqueryui's… May 12, 2026 at 7:37 am
  • Editorial Team
    Editorial Team added an answer Abstract and Generics are completely different things in Java syntax… May 12, 2026 at 7:37 am

Related Questions

I have a fairly well project that I am developing right now, but I
What is the best way to transparently rewrite a URL over an SSL connection
I have to write code to clone a database entry with associated data in
I have been using the MySql Query Browser and [opinion]it has to be the

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.