多个线程使用class.forname(“ ...”)加载不同的类
我有一个方案,我应该在其中监视点击室,而另一个应该监视sqlite。为了加载驱动程序类,我正在使用class.forname(“ ...”)
。由于线程一次开始。我认为这是在僵局状态下。
这是描述该问题的代码。
public class ch {
public static void main(String[] args) throws Exception {
new OTTLCleaner().start(); // loading CH Driver
new CHThr().start(); // loading CH Driver
Spark.getInstance(); // loading Sqlite Driver
}
}
class OTTLCleaner extends Thread {
@Override
public void run() {
try {
System.out.println("Inside OTTL");
DBHelper.deleteTables();
...
} catch (Exception e) {
System.out.println(e);
}
}
}
class CHThr extends Thread {
@Override
public void run() {
try {
System.out.println("Inside CHThr");
DBHelper.CHConn();
...
} catch (Exception e) {
System.out.println(e);
}
}
}
class Spark implements Runnable {
private static class SingletonHelper {
private static final Spark INSTANCE = new Spark();
}
public static Spark getInstance() {
return SingletonHelper.INSTANCE;
}
private Spark() {
try {
System.out.println("Inside Spark");
DBHelper.SqliteConn();
...
} catch (Exception e) {
System.out.println(e);
}
}
@Override
public void run() {
...
}
}
class DBHelper {
public static void CHConn() throws ClassNotFoundException, SQLException {
Class.forName("ru.yandex.clickhouse.ClickHouseDriver");
System.out.println("CH");
...
}
static synchronized void SqliteConn() throws Exception {
Class.forName("org.sqlite.JDBC");
System.out.println("spark");
...
}
public static void deleteTables() throws Exception {
Class.forName("ru.yandex.clickhouse.ClickHouseDriver");
System.out.println("OTTl");
...
}
}
我在线程之间添加了thread.sleep
,它起作用。这是现实世界中的不良方法。我可能会再有一个线程,该线程可能需要在以后加载diff驱动程序。实施此功能的好方法是什么?
I have a scenario in which I thread should monitor Clickhouse and the other one should sqlite. In order to load driver classes, I am using Class.forName("...")
. Since the threads are starting at a time. I think it is going under deadlock situation..
Here is the piece of code that describes the issue.
public class ch {
public static void main(String[] args) throws Exception {
new OTTLCleaner().start(); // loading CH Driver
new CHThr().start(); // loading CH Driver
Spark.getInstance(); // loading Sqlite Driver
}
}
class OTTLCleaner extends Thread {
@Override
public void run() {
try {
System.out.println("Inside OTTL");
DBHelper.deleteTables();
...
} catch (Exception e) {
System.out.println(e);
}
}
}
class CHThr extends Thread {
@Override
public void run() {
try {
System.out.println("Inside CHThr");
DBHelper.CHConn();
...
} catch (Exception e) {
System.out.println(e);
}
}
}
class Spark implements Runnable {
private static class SingletonHelper {
private static final Spark INSTANCE = new Spark();
}
public static Spark getInstance() {
return SingletonHelper.INSTANCE;
}
private Spark() {
try {
System.out.println("Inside Spark");
DBHelper.SqliteConn();
...
} catch (Exception e) {
System.out.println(e);
}
}
@Override
public void run() {
...
}
}
class DBHelper {
public static void CHConn() throws ClassNotFoundException, SQLException {
Class.forName("ru.yandex.clickhouse.ClickHouseDriver");
System.out.println("CH");
...
}
static synchronized void SqliteConn() throws Exception {
Class.forName("org.sqlite.JDBC");
System.out.println("spark");
...
}
public static void deleteTables() throws Exception {
Class.forName("ru.yandex.clickhouse.ClickHouseDriver");
System.out.println("OTTl");
...
}
}
I added Thread.sleep
in between the threads, it works. It is a bad approach in real world. I may get one more thread which may needs to load diff driver in future. What is the good approach to implement this without fail?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论