Processes and Threads

I was having a conversation with a friend of my mine; we were discussing how computers organize the processes and threads. Essentially, how the processors (cores, actually) handle the various tasks required of them.

This led me to my usage of Threads in the tOSU-WebServer and made me feel that some might benefit from a practical, applied overview of the topic.

What are Threads

A computer consists of many components; the components are numerous and truly vary from computer-to-computer. A component that is key to a computer is the central processing unit. A while ago, when I started programing it was almost necessary to understand how a CPU works. These days, this isn’t so much the case.

The tOSU-WebServer utilizes threads; I’d suspect that most web servers would require this though I don’t believe this is a strict requirement.

In order to understand tOSU, however, basic knowledge of threads will certainly be useful. To start, I think it is helpful to understand that computers can only accomplish so much at a given time. At one point, computers could only accomplish one thing at a time; this is quickly fading away as mutlicore processors are quite common these days.

But what does multicore actually mean? A core, in a general sense, is the component that does the actual calculations and logic. When you write: if( j > 0 ) i = j + 2;  A ‘core’ actually handles both the logic and mathematics. This means that a dual core machine can do two of these concurrently, whereas a quad-core can do four of these at any given moment. More is better, but obviously there are dependencies upon previous steps.

A simple example

In order to help initial understanding of a program with multiple threads, I’ve put together a small Java “Hello,World” like program that utilizes multiple threads. This code is basic but should introduce the topics nicely. If you are new to threads, analyzing and running this program on your local computer will be greatly helpful.

The source code can be view directly at: MultiThreadedHelloWorld.java

The output for the program ends up as:

Hello World!
-> Each Thread started. Waiting for all threads to complete
Hello, Becky
Hello, Frank
Hello, Heather
Hello, Jim
Hello, Evan
Hello, Charlie
Hello, Alex
Hello, Greg
Hello, Irene
Hello, Doug
-> Done, exiting

Note that the names were load loaded in to the ArrayList in alphabetical order. This is one of the things of threads.

tOSU-WebServer

Anyway, using that as a basis, we can move on to the actual tOSU-WebServer. The server uses different threads for different tasks. Aside from the processing benefits, this separates code that responds to the client from code that handles incoming connections. The two are different tasks. The code that waits for incoming connections, will spawn (create) a thread to handle the incoming connection.

So, what handles what? The class “MyWebServer” (view) (in the ui package) handles the incoming connections. The “HttpWorker” (view) (in the http package) handles the client request. Lets take a brief look at the code that is actively starting threads.

77
78
79
80
81
82
83
while( true ) {
Socket sock = servsock.accept();
if( _mode == ServerMode.WebServer )
WorkerFactory.newServerWorker(sock, _pathToServeFrom, _dPrinter).start();
else
WorkerFactory.newListener(sock, _dPrinter).start();
}

This snippet is lines 77 though 85 in file MyWebServer.java tagged as v0.5.2. There is actually much more here than we needed but using this actual, running code should make the example easier to follow. Either method, newServerWorker() or newListener() return a class that is a instance of Thread. (Thread in the Java API). Once we call the start() method, the computer returns to line 78 to wait for a new incoming connection and concurrently the thread starts executing. If you recall, it executes the content of the instances’ run() method and this runs regardless if a new connection is received and additional start() methods are called. Each connection is handled independently of the other even if all responses go back to the client.

That is pretty much all there is to it. From the start, it might have sounded complex, but in reality it is simpler. Coordinating large, multi-threaded programs, however, can become complex — particularly in regards to passing data and coordinating such handling. This topic is an entirely different blog entry. Thank you for reading. Please leave comments below.

  • Delicious
  • Facebook
  • Digg
  • Reddit
  • StumbleUpon
  • Twitter