CM10228
/ Programming Ib: Lab
2
Concurrency and Networking: Threading (Concurrency)
<<
Back to contents
Background:
In
the early days of computing when programs were written on punch cards,
you had to book a time slot on the computer in order to get to run your
program. Errors, having to wait for input, or even just someone booking
too much time were hugely inefficient uses of computing power when
there were other programs that needed to be run. Nowadays, operating
systems are ubiquitous, and it is possible to run multiple programs at
the same time with no obvious switching, as far as the user is
concerned. This area of computer science is called concurrency. A
thread is like a 'worker' that runs some instructions. Using multiple
threads allows the processor to allocate time to
different programs and switch between them seamlessly. Furthermore, if
one program is busy waiting for something that the processor can't help
(e.g. user input), then it can spend time on the others instead. But
that's not all, a single program will often have multiple threads. The
primary purpose of this is so that it can handle multiple jobs 'at the
same time'. For instance, a game might have one thread running the GUI,
another running the AI, and another communicating over the network.
Since multi-processor and multi-core systems came along, threads are
also
useful for separating a single large task to be handled more
efficiently by
separate processors (or cores).
Do this:
- Question:
- Can
you explain the two different ways of writing a Thread in Java? What
are their benefits and which is more useful? (The first four pages of
the tutorial should help you here.)
- Task:
- Implement this in Java:
- Write a thread which just prints the numbers
from 1 to 100, prefaced with "Thread: " and then dies. i.e. "Thread:
1", "Thread: 2", etc.
- Create a class with a main method, which
creates and runs your thread. It then goes on to print the numbers from
1 to 100, prefaced with "Main: ", i.e. "Main 1", "Main 2".
You
should see that your thread and your main method take it in
turns
to print their numbers. But notice that it is not perfect, you cannot
rely on them getting the same amount of time to perform their actions.
Only that they will be roughly happening at the same time.
You have
made a thread! It really is that easy. Just this will be enough to get
you far with threading. But you should continue to read and learn about
some other features before we move on.
- Try adding calls to the method
sleep(...) to the program you made in the previous step. (The next page
of the tutorial should help.)
- Try implementing the code that you find on each page up
to and including The
SimpleThreads Example, either by adding
it to your previous program, a new one, or just download the code from
the website. Experiment, it is the best way to learn.
Now you
have learned the basics of threads. This is all you need for this
sheet, however there are a few more topics that are worth learning and
will help you with coursework 2, in particular:
Do this: (in your own time)
But for this lab it is time to move on to networking.
Networking >>
page author: Andrew Chinery
last updated: 01 March 2013