我对 MySQL 和 java 线程的行为感到非常困惑。我声明这是同步的,我得到的结果是冲突的。这意味着多个线程同时访问同一个函数。此代码段位于可运行类中。MachineID 是按照在 for 循环中调用它的顺序的线程 ID。(它只是一个从 1 到 100 的数字)。
我不认为该表是必需的信息
这是我得到的输出
144 18
144 17
144 11
144 13
144 10
144 9
public synchronized int getRow() throws SQLException{
String query="SELECT * FROM Searches WHERE checked='0'";
ResultSet results = this.database.executeQuery(query);
int id=0;
if(results.next()){
id=results.getInt(1);
System.out.println(id+" "+this.machineID);
query = "UPDATE Searches SET checked='1' WHERE ID_num='"+id+"'";
System.out.println(this.database.executeUpdate(query));
}
return id;
}
public void run() {
int id=getRow();
if (id!=0) {
}
}
这是我调用我的线程的地方
for (int i = 0; i < verifier.length; i++) {
verifier[i]=new Thread(new Verifier(main.database,i+1));
verifier[i].start();
}