简体   繁体   English

如何在插入数据库的过程中同时启动和休眠多个线程

[英]how can we start and sleep multiple thread at a same time during inserting in database

i want to execute 4 thread at a same time repeatedly and sleep also at a same time. 我想在同一时间重复执行4个线程,也要同时睡眠。 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();
         }

     }
}

You do not have any control over these threads after you invoke start() methods. 调用start()方法后,您将无法控制这些线程。

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 ! 因此,您无法确定何时执行线程

It's impossible to make multiple threads run exactly the same time from java side cause it's the scheduling responsibility of OS. 使多个线程从Java端完全同时运行不可能的,因为这是OS的调度责任。 If the number of threads is bigger than the number of CPU, there must have some kind of time-sharing. 如果线程数大于CPU数,则必须进行某种分时。 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. 就我所知,即使线程数等于或小于CPU数,也很难使线程在同一时间在不同的CPU上运行。

In java view, you can let a set of threads act at the same state, take CyclicBarrier for an example. 在java视图中,可以让一组线程以相同的状态运行,以CyclicBarrier为例。

Refer to How to start two threads at “exactly” the same time for more details. 有关更多详细信息,请参阅如何“完全”同时启动两个线程

Hope it will help. 希望它会有所帮助。 Thx 谢谢

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM