简体   繁体   English

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

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

Im a bit unsure and have to get advice. 我有点不确定,必须征求意见。
I have the: 我有:

public class MyApp extends JFrame{

And from there i do; 从那里开始

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

MyServer is a: MyServer是:

public class MyServer extends SwingWorker<String, Object> {   

MyServer is doing listen_socket.accept() in the doInBackground() MyServerlisten_socket.accept()中正在执行doInBackground()

and on connection it create a new 并在连接上创建一个新的

class Connection implements Runnable {

I have the belove DbHelper that are a singleton. 我有一个单身人士的挚爱DbHelper

It holds an Sqlite connection. 它拥有一个Sqlite连接。 Im initiating it in the above MyApp 我在上面的MyApp启动它
and passing references all the way in to my runnable: 并将引用一直传递到我的runnable中:

class Connection implements Runnable {

My question is what will happen if there are two simultaneous read or `write? 我的问题是,如果同时进行两个read或写操作,将会发生什么?
My thought here was the all methods in the singleton are synchronized and 我的想法是单例中的所有方法都是同步的
would put all calls in the queue waiting to get a lock on the synchronized method. 会将所有调用放入队列中,等待获得对同步方法的锁定。

Will this work or what can i change? 这项工作还是可以更改?

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 "";

    }

}

Classes in java.sql are required to be thread-safe, according to the spec: 根据规范, java.sql中的类必须是线程安全的:

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

We require that all operations on all the java.sql objects be multi-thread safe and able to cope correctly with having several threads simultaneously calling the same object. 我们要求所有java.sql对象上的所有操作都是多线程安全的,并且能够正确地处理多个线程同时调用同一对象的问题。 Some drivers may allow more concurrent execution than others. 一些驱动程序可能允许比其他驱动程序更多的并发执行。 Developers can assume fully concurrent execution; 开发人员可以假定完全并发执行; if the driver requires some form of synchronization, it will provide it. 如果驱动程序需要某种形式的同步,它将提供它。 The only difference visible to the developer will be that applications will run with reduced concurrency. 开发人员可见的唯一区别是,应用程序将在并发性降低的情况下运行。

This is unlikely to be a good idea. 这不太可能是一个好主意。 You ought to reconsider this design. 您应该重新考虑这种设计。 I think pooling connections and closing them in the narrowest scope possible would be a better idea. 我认为合并连接并在尽可能小的范围内关闭它们是一个更好的主意。

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

相关问题 有人可以告诉我在Java中设置此Robot类时我做错了什么吗? - Can someone tell me what I'm doing wrong to setup this Robot class in Java? JAVA:我做错了什么? - JAVA: What am I doing wrong? 我究竟做错了什么? Java IllegalFormatConversionException - What am I doing wrong? Java IllegalFormatConversionException 我在 Java 中做错了什么? - What am I doing wrong in Java? 用户输入在Java线程中不起作用。 我究竟做错了什么? - User input does not work within a java thread. What am I doing wrong? Java:Paint应用程序,代码不起作用,我在做什么错? - Java: Paint application, the code doesn't work, what am I doing wrong? 试图在 Java 中使用数组,但不知道我做错了什么 - trying to work with arrays in java but don't know what i am doing wrong 官方 Java 练习解决方案不起作用……我做错了什么? - Official Java excercize solution doesn't work… what I am doing wrong? 无法让 documentlistener 工作启用或禁用 Java(Netbeans)中的按钮,我做错了什么? - Unable to get documentlistener to work enable or disable a button in Java (Netbeans), what am I doing wrong? Java GregorianCalendar我做错了什么? 错误的约会? - Java GregorianCalendar What am I doing wrong? Wrong date?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM