繁体   English   中英

Java将这种线程设置工作或我在做什么错

[英]java will this threading setup work or what can i be doing wrong

我有点不确定,必须征求意见。
我有:

public class MyApp extends JFrame{

从那里开始

MyServer = new MyServer (this);
MyServer.execute();

MyServer是:

public class MyServer extends SwingWorker<String, Object> {   

MyServerlisten_socket.accept()中正在执行doInBackground()

并在连接上创建一个新的

class Connection implements Runnable {

我有一个单身人士的挚爱DbHelper

它拥有一个Sqlite连接。 我在上面的MyApp启动它
并将引用一直传递到我的runnable中:

class Connection implements Runnable {

我的问题是,如果同时进行两个read或写操作,将会发生什么?
我的想法是单例中的所有方法都是同步的
会将所有调用放入队列中,等待获得对同步方法的锁定。

这项工作还是可以更改?

public final class DbHelper {

    private boolean initalized = false;
    private String HomePath = "";
    private File DBFile;

    private static final String SYSTEM_TABLE = "systemtable";   
    Connection con = null;
    private Statement stmt;
    private static final DbHelper instance = new DbHelper ();

    public static DbHelper getInstance() {

        return instance;

    }

    private DbHelper () {

        if (!initalized)
        {
            initDB();

            initalized = true;
        }
    }

    private void initDB()
    {
        DBFile = locateDBFile();
        try {

            Class.forName("org.sqlite.JDBC");

            // create a database connection
            con = DriverManager.getConnection("jdbc:sqlite:J:/workspace/workComputer/user_ptpp");

        } catch (SQLException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }

    private File locateDBFile()
    {
        File f = null;
        try{
            HomePath = System.getProperty("user.dir");
            System.out.println("HomePath: " + HomePath);
            f = new File(HomePath + "/user_ptpp");
            if (f.canRead())
                return f;
            else
            {
                boolean success = f.createNewFile();
                if (success) {
                    System.out.println("File did not exist and was created " + HomePath);
                    // File did not exist and was created
                } else {
                    System.out.println("File already exists " + HomePath);

                    // File already exists

                }
            }
        } catch (IOException e) {
             System.out.println("Maybe try a new directory. " + HomePath);
            //Maybe try a new directory.
        }
        return f;
    }

    public String getHomePath()
    {
        return HomePath;
    }



    public synchronized String getSelectedSystemTableColumn( String column) {

        String query = "select "+ column + " from " + SYSTEM_TABLE ;
        try {
            stmt = con.createStatement(ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY);
            ResultSet rs = stmt.executeQuery(query);
            while (rs.next()) {

                String value = rs.getString(column);

                if(value == null || value == "")
                    return "";
                else
                    return value;
            }

        } catch (SQLException e ) {
            e.printStackTrace();
            return "";
        } finally {

        }
        return "";

    }

}

根据规范, java.sql中的类必须是线程安全的:

http://docs.oracle.com/javase/1.3/docs/guide/jdbc/spec/jdbc-spec.frame9.html

我们要求所有java.sql对象上的所有操作都是多线程安全的,并且能够正确地处理多个线程同时调用同一对象的问题。 一些驱动程序可能允许比其他驱动程序更多的并发执行。 开发人员可以假定完全并发执行; 如果驱动程序需要某种形式的同步,它将提供它。 开发人员可见的唯一区别是,应用程序将在并发性降低的情况下运行。

这不太可能是一个好主意。 您应该重新考虑这种设计。 我认为合并连接并在尽可能小的范围内关闭它们是一个更好的主意。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM