依照以下方法使用10个Thread运行时会发生前一个获取result.next()还未处理完就被另一个Thread运行result.next()所以获取的数据不正确,请教由外部传入的result可以怎么设置?public class SimpleThread extends Thread {
public static String filter=";";
public static int i=0;
public ResultSet result;
public static HashMap<String,Vector<HashMap>> hm;
public SimpleThread(ResultSet result,HashMap<String,Vector<HashMap>> hm) {
this.result=result;
this.hm=hm;
} public void run() {
System.out.println("simple start");
Date ttstart = new Date();
SimpleDateFormat ttf = new SimpleDateFormat("yyyy/MM/dd hh:mm:ss");
System.out.println(ttf.format(ttstart));
long arrt1 = System.currentTimeMillis();
try {
while (result.next()) {...............还有其他代码
...............
}
public static String filter=";";
public static int i=0;
public ResultSet result;
public static HashMap<String,Vector<HashMap>> hm;
public SimpleThread(ResultSet result,HashMap<String,Vector<HashMap>> hm) {
this.result=result;
this.hm=hm;
} public void run() {
System.out.println("simple start");
Date ttstart = new Date();
SimpleDateFormat ttf = new SimpleDateFormat("yyyy/MM/dd hh:mm:ss");
System.out.println(ttf.format(ttstart));
long arrt1 = System.currentTimeMillis();
try {
while (result.next()) {...............还有其他代码
...............
}
synchronized(result){ // 获取该行的所有值之前给result对象加锁
if (result.next()) {
int id = result.getInt(1);
// ......
String s = result.getString(10);
}
} // 获取该行的所有值后同步锁解除
//其它代码
} catch (Exception e){}
synchronized(result){ // 获取该行的所有值之前给result对象加锁
if (result.next()) {
int id = result.getInt(1);
// ......
String s = result.getString(10);
}
} // 获取该行的所有值后同步锁解除
//其它代码
} catch (Exception e){}
請教如果以下狀況是否可行?
synchronized(result){
synchronized(A){ }
synchronized(B){ }
}
A B 代表使用的变量但证实当初这样的设计有问题
后来发现其实取到的RESULT应该就是每个THREAD都不同