将RESULT传给10个Thread运行问题
依照以下方法使用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()) {
...............还有其他代码
...............
}
[解决办法]
举例如下:
try { synchronized(result){ // 获取该行的所有值之前给result对象加锁 if (result.next()) { int id = result.getInt(1); // ...... String s = result.getString(10); } } // 获取该行的所有值后同步锁解除 //其它代码 } catch (Exception e){}
[解决办法]
举例如下:
try { synchronized(result){ // 获取该行的所有值之前给result对象加锁 if (result.next()) { int id = result.getInt(1); // ...... String s = result.getString(10); } } // 获取该行的所有值后同步锁解除 //其它代码 } catch (Exception e){}
[解决办法]
没有必要的,因为获取result对象锁的对象只能有一个,一个对象不可能同时获取A,B两个对象的锁的