我有一个DBAdmin
连接到数据库的类,然后还有一些其他类,例如等Article
,Category
它们对数据库执行某些查询。
我在 Swing 应用程序中使用这些类来构建一种小型 Intranet CMS 应用程序。
现在,Category
该类有一堆static
方法,例如addCategory
,editCategory
等等,例如:
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
}
PSDBAdmin
是一个Singleton Class,getInstance
方法返回自调用的实例。
现在,我想要做的是数据库操作应该在与应用程序不同的线程中运行,并且我设法做了一些示例测试,这些测试在 a isrun
时在方法中运行。Thread
started
但问题是我无法指定在run
启动线程时应该在方法中运行哪个方法。
从某种意义上说,例如当addCategory
从外部调用时,该run
方法应该调用addCategory
它内部的方法。
有没有办法将函数作为回调参数从方法传递给 run 方法,addCategory
以便它知道在启动新线程时应该在其中调用哪个函数?还是有一种完全不同的方式来实现我想要做的事情?
PS我在这一点上对Java“相当”陌生,尤其是多线程,所以我可能在这里遗漏了一些东西。