簡體   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