java这个线程设置会工作吗?或者我会做错什么

java这个线程设置会工作吗?或者我会做错什么,java,multithreading,sqlite,Java,Multithreading,Sqlite,我有点不确定,必须征求意见。 我有: public class MyApp extends JFrame{ 从那里我做到了 MyServer = new MyServer (this); MyServer.execute(); MyServer是一个: public class MyServer extends SwingWorker<String, Object> { 我有一个名为singleton的beloveDbHelper 它拥有一个Sqlite连接。我正在上面的

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

public class MyApp extends JFrame{
从那里我做到了

MyServer = new MyServer (this);
MyServer.execute();
MyServer
是一个:

public class MyServer extends SwingWorker<String, Object> {   
我有一个名为singleton的belove
DbHelper

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

class Connection implements Runnable {
我的问题是,如果同时有两个
读取
或`写入?
我的想法是,singleton中的所有方法都是同步的,
将把所有调用放在队列中,等待获得同步方法的锁

这行吗?我能改变什么

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
中的类必须是线程安全的:

我们要求所有java.sql对象上的所有操作都是 多线程安全,能够正确处理多线程 线程同时调用同一对象。有些司机可能会允许 更多的并发执行。开发人员可以充分假设 并行执行;如果驱动程序需要某种形式的 同步,它将提供它。唯一的区别是 开发人员希望应用程序能够以更少的内存运行 并发性


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

@Erik,单例模式确保只创建一个项目,而不是不做任何保证;并发是正交的。不,单例反模式与线程安全无关。一个易变的、不同步的单例将是一个非常头痛的问题。如果我使用MySql而不是Sqlite,并且使用相同的setupDatabase供应商,那会怎么样呢?@duffymo谢谢,现在我将java.util.concurrent.locks.Lock放在方法关键部分。而是想为我的桌面java应用程序尝试h2database.com的嵌入式版本。我是个笨蛋,你怎么想?
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 "";

    }

}