在mysql中插入记录导致线程数增加

时间:2019-01-23 08:17:59

标签: java mysql multithreading java-threads

我创建了两个线程“ READ_THREAD”和“ WRITE_THREAD”,它们以一定的睡眠间隔连续运行,如下面的代码所示。当我运行此代码而没有数据库插入查询和打印线程计数时,它始终给出3。但是,当我在代码中添加插入查询时,线程数增加到4,并且随着时间的流逝……它一直在增加。我在做什么错?

public class test {

    public static void main(String[] args) {
        SingcomUC1SerialRunnableThreadClass t1 = new SingcomUC1SerialRunnableThreadClass("READ_THREAD");
        t1.start1();
        SingcomUC1SerialRunnableThreadClass t2 = new SingcomUC1SerialRunnableThreadClass("Write_THREAD");
        t2.start1();
        System.out.println("active thread count=" + Thread.activeCount());
    }
}

public class SingcomUC1SerialRunnableThreadClass implements Runnable {

    Thread t;
    String threadName;
    String singcomUC1Readthread="READ_THREAD";
    String singcomUC1Writethread="Write_THREAD";


    public SingcomUC1SerialRunnableThreadClass(String name) {
        threadName = name;
    }

    public void start1() {
        if (t == null) {
            t = new Thread(this, threadName);
            t.start();
        }
    }

    @Override
    public void run() {
        System.out.println("run() invokations");
        while (true) {
            if (threadName.equalsIgnoreCase(singcomUC1Readthread)) {
                System.out.println(Thread.currentThread().getName() + "--" + Thread.activeCount());
                try {
                    commonSection();
                } catch (InterruptedException ex) {
                    Logger.getLogger(SingcomUC1SerialRunnableThreadClass.class.getName()).log(Level.SEVERE, null, ex);
                }
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException ex) {
                    Logger.getLogger(SingcomUC1SerialRunnableThreadClass.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
            if (threadName.equalsIgnoreCase(singcomUC1Writethread)) {
                System.out.println(Thread.currentThread().getName() + "--" + Thread.activeCount());
                try {
                    commonSection();
                    Thread.sleep(300);
                } catch (InterruptedException ex) {
                    Logger.getLogger(SingcomUC1SerialRunnableThreadClass.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        }
    }

    public void commonSection() throws InterruptedException {
        InsertValuesIntoUC1DBtable.insertDefaultValueAndStatusIntoUC1DBtable(singcomUC1DeviceId);
        ReadOrUpdateUC1CONTROLtable.insertDefaultValueAndStatusIntoDC1DBtable(singcomUC1DeviceId);
    }
}

public class InsertValuesIntoUC1DBtable {

    public static void insertDefaultValueAndStatusIntoUC1DBtable(int deviceid) {
        try {
            Connection con = null;
            DatabaseConnection databaseConnection_obj = new DatabaseConnection();
            con = databaseConnection_obj.getConnection();
            PreparedStatement pst = con.prepareStatement("select * from uc1db");
            ResultSet rs = pst.executeQuery();
            if (!rs.next()) {
                pst = con.prepareStatement("insert into uc1db(deviceid,frequency,attenuation,transmitterstatus,devicestatus) values(?,?,?,?,?)");
                pst.setInt(1, deviceid);
                pst.setString(2, "0");
                pst.setString(3, "0");
                pst.setString(4, "0");
                pst.setString(5, "online");
                pst.executeUpdate();
            }
            rs.close();
            pst.close();
            con.close();
        } //**
        catch (SQLException ex) {
            Logger.getLogger(InsertValuesIntoUC1DBtable.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}

public class DatabaseConnection {

    public Connection getConnection() {
        Connection con = null;
        try {
            Class.forName("com.mysql.jdbc.Driver");
            con = DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "root", "admin");
        } catch (Exception e) {
            System.out.println(" Db Exception  " + e);
        }
        return con;
    }
}

输出:

active thread count=3
run() invokations
run() invokations
singcomUC1ReadThread--2
singcomUC1WriteThread--2
singcomUC1WriteThread--4
singcomUC1WriteThread--4

1 个答案:

答案 0 :(得分:0)

那么,您问的那些难以捉摸的话题是什么?

通过运行Thread.currentThread().getThreadGroup().list()

可以获得一些见解。

这就是输出的样子:

java.lang.ThreadGroup[name=main,maxpri=10]
    Thread[READ_THREAD,5,main]
    Thread[Write_THREAD,5,main]
    Thread[DestroyJavaVM,5,main]
    Thread[mysql-cj-abandoned-connection-cleanup,5,main]

您可能认识到的前两个。

DestroyJavaVM是一个维护线程,可确保在终止VM之前已完成所有其他线程。

mysql-cj-abandoned-connection-cleanup非常相似,但用于MySQL Connector。

请注意,您不必担心那些维护线程。它们通常由非常熟练的人员编写,以确保这些线程不会影响应用程序的性能。您应该更担心的是您自己的线程,更具体地说,不要使用线程池来管理它们。