Java Reference
In-Depth Information
In this chapter you'll learn more about objects, classes, and scheduling
tasks that will run later. You'll add this knowledge to your toolbox:
• How to create a separate class
• How to schedule a task object to run later
• How to schedule a task object to keep running later
Your toolbox is nearly complete now. You can write some really cool
plugins just based on what we've done so far.
Schedule Tasks for Later
Now we'll talk about how to make things happen in Minecraft that aren't in
direct response to either an event or a user-issued command: how to schedule
tasks that will happen sometime in the future—and even keep running—all
on their own. This feature helps you implement things that seem to act
independently, like attacking creepers or other enemies.
On the Java front, you'll also see how to make your own classes in separate
files, and we'll take a look at some of the problems Java has with running
multiple things at once.
What Happens When?
When we talk about running something “later,” it exposes the ugly truth that
the world doesn't stop changing just because your code is running. Computers
can do more than one thing at once, and are doing that all the time. But most
code isn't written that way.
Think about some of the plugins we've looked at so far. What would happen
if, right in the middle of running that piece of code, another player typed the
same command and that same code started running from the top?
Take a look at the figure on page 136 . Each arrow and code snippet represents
a set of instructions that the computer is running at the same time. We call
each separate set a thread —kind of like the thread of a subplot in a novel, or
the thread of one conversation in the middle of many others at lunch.
Normally you think of the computer running through your code once, as in
the left part of the diagram. You'd think the computer executes instructions
one after another, from top to bottom, and nothing else is going on at the
same time. That would be one thread of execution (like one conversation).
Search WWH ::

Custom Search