i want to execute 4 thread at a same time repeatedly and sleep also at a same time. but problem is that after getting few right sequence data, few data showing different time. I'm getting following output
dtime data
21-MAY-16 09.38.31.031000 AM .2652487
21-MAY-16 09.38.31.031000 AM .21100356
21-MAY-16 09.38.31.031000 AM .39925393
21-MAY-16 09.38.31.031000 AM .32884327
21-MAY-16 09.38.33.046000 AM .08516244
21-MAY-16 09.38.33.046000 AM .701089
21-MAY-16 09.38.33.046000 AM .9537386
21-MAY-16 09.38.33.086000 AM .0397367
Here is the snippet as follows:
public class TestConnection {
public static void main(String[] args) {
Thread t1=new Thread(new TestingThread(),"t1");
Thread t4=new Thread(new TestingThread(),"t4");
t1.start();
t4.start();
}
}
public class TestingThread implements Runnable{
@Override
public void run(){
try{
doDB();
}
catch(InterruptedException e){
System.out.println(e);
}
}
private void doDB() throws InterruptedException {
Connection conn;
PreparedStatement pstmt;
try
{
/* Create Connection objects */
conn= ....;
/* Create the insert statement */
String insertQuery = ".....";
pstmt = conn.prepareStatement(insertQuery);
for(int i=1;i<3;i++)
{
pstmt.setTimestamp(1, getCurrentTimeStamp());
pstmt.setFloat(2, (float) Math.random());
i=pstmt.executeUpdate();
}
try{
if(conn!=null){
conn.commit();
conn.close();
}
}
catch(SQLException se){
se.printStackTrace();
}
}
catch(Exception e)
{
e.printStackTrace();
}
}
}