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.