ashishgupta_mca
ashishgupta_mca

Reputation: 578

Regarding stopwatch or timer or some other utility

I have a requirement to start a task..Now many threads can start this task and this task normally takes 4-5 seconds to complete. I want to prevent the starting of a task if this task has been already started by some other thread. In order to implement this requirement, I am thinking of starting a timer or stopwatch in a different thread whenever the task is started by some thread. Now when the timer times out after a configured time-interval, another thread can starts a task. So, is starting a timer or stopwatch in a different thread to see if the particular time has been reached is a good solution?Is there any good alternative for it?

Upvotes: 0

Views: 157

Answers (3)

Tomasz Nurkiewicz
Tomasz Nurkiewicz

Reputation: 340733

If I understand correctly, this is a bad idea. Basically you are assumming your job will never run for more than 5 seconds so if the watch tells you that some job was started less than 5 seconds ago, you won't start another one. This is very unreliable.

Instead create some sort of flag that you set when job starts and unset when ends. AtomicBoolean is perfect for that:

private AtomicBoolean flag = new AtomicBoolean();

//...

if(!flag.getAndSet(true)) {
    try {
        //do your work
    } finally {
        flag.set(false);
    }
} else {
    //Already running
}

If you want another job to wait for the previous one instead of simply being discarded, just surround your task with synchronized or use some different locking mechanism.

Note: if your jobs are distributed you will need a distributed locking mechanism, like a databasse or .

Upvotes: 1

user_CC
user_CC

Reputation: 4776

If you are trying to do this in java then you can consider using a synchronized block on the Object Oriented approach on JAVA.

So any task that you want to make sure is done by one thread at a time then make a class and a synchronized method in that class, also make sure you all the threads share the same object of the class and call this method in which they want to perform the task.

For Example

Class SyncTask{


synchronized void task1(){

//Perform your task here


}

}

Create the object of this class once during the lifetime of your application and then use this same object across all the threads and let them call this method to which you want to perform your task.

In the case of multiple threads invoking this method at the same time. JVM will take care of the sequence and if one thread is already performing a task, the others calling it will wait for the first one to finish.

In this way you will be sure that only on thread is performing the task at any given time.

I hope this helps.

Upvotes: 0

npinti
npinti

Reputation: 52185

If you want to schedule task the framework of choice is usually something similar to Quartz. It should allow you to do what you need and more. Regarding the issue of non running concurrent tasks, I would recommend you take a look at this previous SO post which should point you in the right direction.

Upvotes: 0

Related Questions