public class MianShiTi implements Runnable{
/**
* 自增ID
*/
private static int incrementId = 1;
public synchronized static int getincrementId(){
return incrementId++;
}
@Override
public void run() {
int id = 0;
while((id=getincrementId())<=1000){
System.out.println("插入数据主键:"+id+" 当前线程"+Thread.currentThread().getName()+" ");
}
}
public static void main(String[] args) {
for (int i = 1; i <=10; i++) {
Thread thread = new Thread(new MianShiTi());
thread.setName("线程"+i);
thread.start();
}
}
}
|