Homepage
Amazon.com Wish List
Ole Miss on Google Maps
Tags for this wiki
Most Recent Changes
Have a question about anything on this site?
E-mail me at ![]()
Any time you need to make a computer perform two or more tasks at the same time, this is called "concurrency". Most applications that you use on your computer will run along side other applications. It is common for me to have a text editor open with code at the same time for me to be playing internet radio. (Also, while writing this lesson, I was burning the Ubuntu ISO to to CD.)
My computer had to organize the following hardware devices:
The keyboard (because I was typing)
The mouse (so I can navigate around my computer)
The monitor (because I had to see what I was typing)
The sound card (so I can hear the 10,000 Maniacs)
The CD drive (so it can burn a CD)
The computer also as to organize the following software processes:
Firefox (For my e-mail)
Flash (So I can access Pandora)
GNOME Terminal (So I can access the command line)
VI (My text editor)
The File Manager (So I can see the Ubuntu ISO file)
The Disk Writing Software (Which coordinates the CD Burning)
Screen (A piece of software that allows me to have multiple command lines open)
SSH (I'm connected to another computer at the time of writing and left it on all night)
Even within most of these programs, there exist threads which operate within each process:
My text editor has a spell checker that works as soon as I type a misspelled word. There exist at least two threads: one for me to type words into the editor window and a second thread which scans the entire document looking for misspelled words. The two threads work together (in the sense that they both help me write what I need to write) and yet they work independently of each other.
There are two kinds of execution:
processes - These are synonymous with a program running on your computer. They have their own memory space. They are (usually) independent of other processes on your computer. It is possible to communicate back and forth between processes.
threads - These are units of executable code that share a memory space and still run independently. Every processes technically has one thread. Every Java processes has at least two threads: one for the main program execution, and a second processes to represent the garbage collection.
Most concurrent Java programs use Threads. And that is what I'll be demonstrating today.
To create a thread, you create an object that implements Runnable, which is an interface in Java. In your class, you specify a method called "run" and it contains the code that you want to run.
After that, you create your object, pass it to a Thread object, and then call the "start" method of the Thread object. It's a little confusing, but let's illustrate it.
class MyThread implements Runnable {
public void run() {
System.out.println("Hello, world!");
}
public static void main(String[] args) {
MyThread a = new MyThread();
Thread t = new Thread(a);
t.start(); // Calls run, which prints "Hello, world!"
}
}
More here: http://java.sun.com/docs/books/tutorial/essential/concurrency/index.html
Page Information
|
Wiki Information |
Recent PBwiki Blog Posts |