多个线程使用class.forname(“ ...”)加载不同的类

发布于 2025-02-10 03:34:14 字数 2127 浏览 2 评论 0原文

我有一个方案,我应该在其中监视点击室,而另一个应该监视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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文