线程同步问题,急,在线等。。。。

startcool 2010-04-15 02:01:21
public class ExaThread extends GeneralLogic{

public synchronized String test(){
Connection conn = null;
try {
conn = this.getConnection();
Statement stmt = conn.createStatement();
String today = CommonTools.getNumToday();
int maxID = 0;
String sql = "select max(barcode) as bar from qqq where barcode like '" + today + "%'";
ResultSet rs = stmt.executeQuery(sql);
while (rs.next()) {
String tmp = rs.getString("bar");
maxID = Integer.parseInt(tmp == null ? "00000" : tmp.substring(8));
}
rs.close();
String temp = today + CommonTools.leftFillChar(Integer.toString(maxID + 1), "0", 5);
String insql = "insert into qqq values("+temp+")";
stmt.execute(insql);
stmt.close();
return temp;
}
catch (Exception ex) {

}
finally {
this.freeConnection(conn);
}
return null;
}

}


public class Test implements Runnable{
ExaThread et = new ExaThread();
public void run() {

for(int i=0;i<50;i++){
try {
//Thread.sleep(1000);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

System.out.println(Thread.currentThread().getName()+" "+et.test());
}
}

public static void main(String[] args){
Thread t1 = new Thread(new Test());
Thread t2 = new Thread(new Test());
Thread t3 = new Thread(new Test());
Thread t4 = new Thread(new Test());
Thread t5 = new Thread(new Test());
t1.start();
t2.start();
t3.start();
t4.start();
t5.start();
}
}

加了锁,还是出现重号问题。。大家帮看看那里错了。。
...全文
127 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
SambaGao 2010-04-15
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 wyhack 的回复:]
让一个实例启动多个线程,而不是多个实例分别启动一个线程


Java code
Test t = new Test();
new Thread(t).start();
new Thread(t).start();
new Thread(t).start();
new Thread(t).start();
new Thread(t).start();
[/Quote]

看来已经明白怎么回事了。哈哈。
SambaGao 2010-04-15
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 wyhack 的回复:]
让一个实例启动多个线程,而不是多个实例分别启动一个线程


Java code
Test t = new Test();
new Thread(t).start();
new Thread(t).start();
new Thread(t).start();
new Thread(t).start();
new Thread(t).start();
[/Quote]

看来已经明白怎么回事了。哈哈。
WYhack 2010-04-15
  • 打赏
  • 举报
回复
让一个实例启动多个线程,而不是多个实例分别启动一个线程

Test t = new Test();
new Thread(t).start();
new Thread(t).start();
new Thread(t).start();
new Thread(t).start();
new Thread(t).start();
JaredJared 2010-04-15
  • 打赏
  • 举报
回复
这样改简
public static void main(String[] args){
Test t = new Test();
Thread t1 = new Thread(t);
Thread t2 = new Thread(t);
Thread t3 = new Thread(t);
Thread t4 = new Thread(t);
Thread t5 = new Thread(t);

t1.start();
t2.start();
t3.start();
t4.start();
t5.start();
}
}
qery 2010-04-15
  • 打赏
  • 举报
回复
public class ExaThread extends GeneralLogic{

public synchronized String test(){
wait();
Connection conn = null;
try {
conn = this.getConnection();
Statement stmt = conn.createStatement();
String today = CommonTools.getNumToday();
int maxID = 0;
String sql = "select max(barcode) as bar from qqq where barcode like '" + today + "%'";
ResultSet rs = stmt.executeQuery(sql);
while (rs.next()) {
String tmp = rs.getString("bar");
maxID = Integer.parseInt(tmp == null ? "00000" : tmp.substring(8));
}
rs.close();
String temp = today + CommonTools.leftFillChar(Integer.toString(maxID + 1), "0", 5);
String insql = "insert into qqq values("+temp+")";
stmt.execute(insql);
stmt.close();
return temp;
}
catch (Exception ex) {

}
finally {
this.freeConnection(conn);
notifyAll();
}
return null;
}

}


public class Test implements Runnable{
ExaThread et = new ExaThread();
public void run() {

for(int i=0;i<50;i++){
try {
//Thread.sleep(1000);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

System.out.println(Thread.currentThread().getName()+" "+et.test());
}
}

public static void main(String[] args){
Thread t1 = new Thread(new Test());
Thread t2 = new Thread(new Test());
Thread t3 = new Thread(new Test());
Thread t4 = new Thread(new Test());
Thread t5 = new Thread(new Test());
t1.start();
t2.start();
t3.start();
t4.start();
t5.start();
}
}
soli11722984 2010-04-15
  • 打赏
  • 举报
回复
当然重,你都NEW了5个实例出来


Test t = new Test();
Thread t1 = new Thread(t);
Thread t2 = new Thread(t);
Thread t3 = new Thread(t);
Thread t4 = new Thread(t);
Thread t5 = new Thread(t);

62,621

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧