Reputation: 655
I'm creating a twitter bot at the moment.
I have a listener listening for a status that contains a certain hashtag - #DealMeIn. When a status is found with this hashtag, I have a method that creates a new thread, that plays a game:
public void onTweet() {
Thread t = new Thread(new Game());
t.start();
}
The problem with the above method, if I'm not mistaken, is that t will be overwritten each time a tweet is found. So, I'm thinking a suitable alternative would be to use an ArrayList<Thread> threads
as such:
public void onTweet() {
threads.add(new Thread(new Game()));
threads.get(threads.size()-1).start();
}
Is this a suitable way to handle my threads?
Edit: If a user tweets, for instance, #DealMeOut then I need to be able to stop the thread.
Upvotes: 0
Views: 86
Reputation: 14688
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:
Executors
will create and return objects of ExecutorService
(please note that ExecutorService
is an interface). newCachedThreadPool()
, newFixedThreadPool(int nThreads)
you will get an object of ThreadPoolExecutor
(please note that ThreadPoolExecutor
implements ExecutorService
interface).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.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.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());
}
Upvotes: 2
Reputation: 44200
I would create a map of users to the thread which their game is taking place on.
private Map<String, Thread> games = new HashMap<>();
public void onTweet(String user) {
if (!games.containsKey(user)) // if they haven't got a game running
{
Thread t = new Thread(new Game());
t.start();
games.put(user, t);
}
}
public void onStop(String user) {
if (games.containsKey(user))
{
games.remove(user).interrupt();
}
else
{
//should we handle this?
}
}
You will need to ensure the thread is prepared to deal with interrupts.
Upvotes: 0