Answer given by @Michael is fine but I would prefer to get this job done by using Java's executor service, because it gives more control, elegant way to do this and that's what Java provided API to deal with these kind of situations.
See below code example. Clear advantage of this approach is that you can control maximum number of threads running in your JVM, which is extremely important otherwise over period of time your application may start clocking, so this is gives more scalability to your application/solution.
If you are not aware of Java's executor service then below are few quick points to get you started:
- Java's utility class
Executors
will create and return objects of ExecutorService
(please note that ExecutorService
is an interface).
- Now in case of
newCachedThreadPool()
, newFixedThreadPool(int nThreads)
you will get an object of ThreadPoolExecutor
(please note that ThreadPoolExecutor
implements ExecutorService
interface).
- When you do
Executors.newFixedThreadPool(2);
, you only get an object of ThreadPoolExecutor
with all instance variables like core pool size, max pool size etc. set, and in your case it will be set to 2.
- Now, with
private final static ExecutorService executorService = Executors.newFixedThreadPool(2);
you will always have max of 2 threads in your JVM for this thread pool, and any excess request will get queued up in LinkedBlockingQueue
implementation, and since LinkedBlockingQueue
is a unbounded queue so you will never loose your task.
- Suppose you want to create more threads when you have more load then you have to play around with
maximumPoolSize
and use a bounded queue. So, depending on your system capacity / application load you can start with corePoolSize
of let say 100 and set maximumPoolSize
as Integer.MAX_VALUE
.
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
public class ExecutorServiceImplementationExample {
private final static int MAX_NUMBER_OF_THREADS = Integer.MAX_VALUE;
// set maximum number of threads as per your requirement/performance tuning, for testing set it to "2" and to have better feel.
private final static ExecutorService executorService = Executors.newFixedThreadPool(MAX_NUMBER_OF_THREADS);
public static void main(String[] args) {
System.out.println("### Starting.");
new Thread(){
@Override
public void run() {
scheduleTask(new MyRunnableTask());
}
}.start();
new Thread(){
@Override
public void run() {
scheduleTask(new MyRunnableTask());
}
}.start();
new Thread(){
@Override
public void run() {
scheduleTask(new MyRunnableTask());
}
}.start();
new Thread(){
@Override
public void run() {
scheduleTask(new MyRunnableTask());
}
}.start();
System.out.println("### Completed.");
}
private static void scheduleTask(Runnable runnable) {
executorService.execute(runnable);
}
}
MyRunnableTask.java
public class MyRunnableTask implements Runnable {
@Override
public void run() {
System.out.println("I am getting executed: " + this.hashCode() + " | " + Thread.currentThread().getId());
try {
Thread.sleep(2000); // this sleep is only for testing to give a feel of how solution will work, remove after testing.
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
In your case you will do something like below, and you need to have executorService
created in the start and your Game
class should implement Runnable
interface.
public void onTweet() {
executorService.execute(new Game());
}