[TUTORIAL] JAVA - Using timers on L2J

Find all FAQs, HOWTOs and guides here.
Forum rules
READ NOW: L2j Forums Rules of Conduct
Post Reply
User avatar
theone
Posts: 1384
Joined: Tue Aug 12, 2008 2:28 am
Location: Quebec, Canada

[TUTORIAL] JAVA - Using timers on L2J

Post by theone »

I decided to make another small tutorial to help people who are new to Java understand how the ThreadPoolManager works and how to use it. I hope it's useful for some people!

First of all, to all of you reading this and new to Java and/or L2J, L2J has a class already made to handle timers/tasks. This class is called ThreadPoolManager.
I will not get into details as to how it works since this is meant to be a newbie tutorial, so just see the ThreadPoolManager as something that can allow you to set timers and repeating timers for specific tasks.

1- What do I need to know to make the ThreadPoolManager work for me?
Nothing too fancy really. The one thing that you need to know is that the task you want to perform needs to be a Runnable task(I will explain in a minute what this is and how to set it up). Except for this, there is really nothing complicated to know.

2- Runnable tasks:
A Runnable task is basically a small class which implements the Runnable class. For those of you who read my Java 101 tutorial, it's what I called a threadable task.
Lets create a simple one as an example:

Code: Select all

class MyTask implements Runnable{    public void run()    {        // You put your actions here    }}
Without getting into details, you need to at least know that a Runnable task NEEDS to contain the public void run() part. It's essential as it is the part of the code that will be triggered when the task is launched.
The ThreadPoolManager is made to handle many different variations of scheduled tasks, we will cover 2 here, a simple scheduled task and a repeating scheduled task.
First lets see a normal scheduled task which will execute only once.
This is the method we will use:

Code: Select all

ThreadPoolManager.getInstance().scheduleGeneral(Runnable, delay)
The delay is in milliseconds.
Obviously we now need to pass into it the runnable we want to use, lets take our MyTask example, it would give something like this:

Code: Select all

ThreadPoolManager.getInstance().scheduleGeneral(new MyTask(), 5000);
Here I just told the ThreadPoolManager to start the MyTask run() method in 5 seconds(5000 milliseconds).

Now, lets say we want this task to run each 5 seconds in a continuous loop.
We have this method in ThreadPoolManager which can help:

Code: Select all

ThreadPoolManager.getInstance().scheduleGeneralAtFixedRate(Runnable, initial, delay)
The code would then look like this:

Code: Select all

ThreadPoolManager.getInstance().scheduleGeneralAtFixedRate(new MyTask(), 5000, 5000);

3- Passing variables to a Runnable task:
Our Runnable tasks can perform all sorts of tasks, some of which need to have specific parameters or need to affect something in particular, which is why we often want to pass to the task some informations and/or variables.
Lets use the MyTask example again but expand it a little bit:

Code: Select all

class MyTask implements Runnable{    private L2PcInstance player;    public MyTask(L2PcInstance p)    {        player = p;    }    public void run()    {        player.breakAttack();    }}
In order to add this task to the ThreadPoolManager we must now pass into it the information that the task needs to run, in this case the instance of L2PcInstance(the player) who will be affected by it.
It would look something like this:

Code: Select all

ThreadPoolManager.getInstance().scheduleGeneral(new MyTask(player), 5000);

I could go much more in depth with timers, but I feel that this is enough for most people who are learning L2J development to get them started. Have fun!
Image
User avatar
Tan
L2j Veteran
L2j Veteran
Posts: 873
Joined: Wed Jun 10, 2009 10:31 pm
Location: Poland

Re: [TUTORIAL] JAVA - Using timers on L2J

Post by Tan »

you're awesome =) thanks for another cool guide
Some people believe in God... i believe in Music... some people pray..... I turn on Winamp
http://www.last.fm/user/L2jTan
User avatar
ThePhoenixBird
L2j Inner Circle
L2j Inner Circle
Posts: 1857
Joined: Fri May 27, 2005 5:11 pm

Re: [TUTORIAL] JAVA - Using timers on L2J

Post by ThePhoenixBird »

Moved to FAQ
Post Reply