Friday, 21 October 2011

Java: Concurrency with Threads (Intro)

This is a bit late, and short, as I have unexpected company over :/
Hey, at least its the introductory section, so it's naturally short!

Do date, our only programs have done one thing at a time, which is a little limited. It allows us to actually do pretty cool things, perform calculations etc. But the more interesting programs, such as simple games (and actual important things) would need us to do many things at once. As y'know, we've mentioned.

Consider waiting in an airport lounge for a flight. Number 1, we're waiting. This, while not really an action, needs to be kept track of by checking the departure boards for information about when to board. With the rare exception (I assume), you don't sit around staring at the screen. they find something else to do, reading a newspaper, trying not to murder the loud little shithead kids a couple of seats over, etc. The principle activity is now trying not to be a murderer, or reading, but intermittently, you're going to be looking at the departure board to see if your flight information has changed. If it has, you take some action, like getting up to move towards your flight, if not, you continue seething while reading a magazine. Since multi-tasking is technically impossible (rather, it's the quick flitting between multiple actions), you're only doing one thing at any given moment, while being able to change this thing whenever.
On a more abstract level, though, you're doing multiple things at once. Reading and checking are happening at (sort of) the same time.
TL;DR: Waiting for a plane, you're not constantly staring at flight information, but doing other stuff. You can only do 1 at a time, but on an abstract level, it seems to be 2 things at once. This is generally how multitasking and multi-threading work.

So what algorithm is gonna be used? Since you're flipping between two things, so you could read one page then check info, or whatever. Alternatively, we could be reading, when an event occurs, causing us to look at the board, such as an announcement. At any one time, you are doing one thing, but there are mnay activities planned and waiting, where events cause you to change from one activity to another.

Applying this to programming, this multiple occurrence idea is termed concurrency. Running multiple programs at the same time (internet, media player, whatever) is an example of this. Specifically, multi-tasking.


Questions welcome! Comments, too!  Follow, subscribe, share etc, and see you tomorrow!

And as part of a shameless plug for a friend, if you're interested in classic movies/books/music, visit his site here (fixed), and feel free to throw loads of criticism at us. 

1 comment:

  1. Would like you to teach us how to make our own videogames. (: That would be awesome.