我知道互联网上已经有很多类似的问题,但我的问题是关于我的代码,而不是关于线程。我正在制作一个包含玩家数据库的小应用程序。数据存储类的代码如下。
public class DataManager
{
static final int NO_OF_COLUMNS = 18;
static QDatabase pdb;
public DataManager()
{
pdb = new QDatabase(NO_OF_COLUMNS);
}
public void addPlayer(Object[] playerData)
{
pdb.add(playerData);
}
public void editPlayerInfo(int type, int playerRegNo, Object data)
{
pdb.set(type, playerRegNo, data);
}
public int getPlayerRegNo(String userID)
{
return (int) pdb.getData(USER_ID, userID, REG_NO);
}
public Boolean contains(int column, Object data)
{
return pdb.contains(column, data);
}
}
我有一台服务器,它不断接收来自多个客户端的请求并为每个客户端创建一个新线程。他们都访问这个本质上充当数据库的 DataManager 类。我是否有可能以某种方式使所有线程能够同时调用addPlayer()
和editPlayerInfo()
方法,但又不会因为同步问题而搞砸整个事情?
我也知道我可以使用数据库。但是在这里,我只是认为这会更容易。假设将有大约 200 个线程同时运行。我解决这个问题的最佳方法是什么?
有什么方法可以让所有线程同时访问它,否则有 200 个线程相互等待可能会变得非常慢?
编辑 1: QDatabase 类如下:
public class QDatabase implements Serializable
{
private ArrayList<ArrayList<Object>> database;
public final int NOT_EXISTS = 0, REGULAR = 0, TRANSPOSE = 1;
private int lastid = -1;
//Initializer taking the number of columns as an argument
public QDatabase(int noofcolumns)
{
database = new ArrayList<ArrayList<Object>>();
addColumns(noofcolumns);
}
//Method that adds an array of objects as a new row in the database.
public void add(Object[] object)
{
for(int index = 0; index < database.size(); index++)
{
if(object != null)
{
database.get(index).add(object[index]);
lastid = database.get(0).indexOf(object[0]);
}
}
}
//Method that finds the row in a column where an instance of a particular object is found and get the values at a
//cell with the same row and a given column.
public Object getData(int columntocheck, Object check, int columntoget)
{
Object ramobject = null;
int loc = database.get(columntocheck).indexOf(check);
ramobject = database.get(columntoget).get(loc);
return ramobject;
}
//Method to check if a column contains an instance of a given object.
public Boolean contains(int column, Object objecttocheck)
{
return database.get(column).contains(objecttocheck);
}
//Method to set a given cell to an object.
public void set(int column, int row, Object object)
{
database.get(column).set(row, object);
}
}