0

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();
         }

     }
}
ARoy
  • 181
  • 3
  • 22
  • 2
    Welcome to multi-threading world. You can never be sure about the sequence of execution of threads. If you want to insert records in a sequence, why do you need multiple threads? – justAbit Jul 21 '16 at 04:29
  • "real time Java" is the technical jargon for special editions of Java that have deterministic behavior in terms of timing and scheduling. These special editions are indeed *very* special: academic research projects and niche commercial products. The mainstream Java implementations such as from Oracle and OpenJDK are *not* “real-time Java”. – Basil Bourque Jul 21 '16 at 06:52

2 Answers2

1

You do not have any control over these threads after you invoke start() methods.

It is the underlying OS which takes care of running these threads and most of the time (Especially if number of cores < number of threads executing simultaneously) two threads do not run in parallel.

So, you cannot be certain when a thread will be executed!

Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154
Ouney
  • 1,164
  • 1
  • 10
  • 22
1

It's impossible to make multiple threads run exactly the same time from java side cause it's the scheduling responsibility of OS. If the number of threads is bigger than the number of CPU, there must have some kind of time-sharing. Even if the number of threads is equal or smaller than the number of CPU, in my limited knowledge, it's still hard to make threads run at different CPU at exactly the same time.

In java view, you can let a set of threads act at the same state, take CyclicBarrier for an example.

Refer to How to start two threads at “exactly” the same time for more details.

Hope it will help. Thx

Community
  • 1
  • 1
Eugene
  • 10,627
  • 5
  • 49
  • 67