Worker Thread in Java

后端 未结 4 2173
天命终不由人
天命终不由人 2021-02-08 07:13

I need to read the data from a table every minute through thread & then perform certain action.

Should I just start a thread & put it in sleep mode for 1 minute

4条回答
  •  渐次进展
    2021-02-08 07:47

    Your approach is good.You can proceed your approach.

    This Sample will help you to do your task:

    class SimpleThread extends Thread {
       public SimpleThread(String str) {
        super(str);
       }
       public void run() {
        for (int i = 0; i < 10; i++) {
            System.out.println(i + " " + getName());
            try {
                 // Add your customized code here to update the contents in the database.
    
                sleep((int)(Math.random() * 1000));
    
            } catch (InterruptedException e) {}
        }
        System.out.println("DONE! " + getName());
    }
    }
    

提交回复
热议问题