14: Multiple threads | CodeGuru

14: Multiple threads

Bruce Eckel’s Thinking in Java Contents | Prev | Next Objects provide a way to divide a program up into independent sections. Often, you also need to turn a program into separate, independently-running subtasks. Each of these independent subtasks is called a thread, and you program as if each thread runs by itself and has […]

Written By
CodeGuru Staff
CodeGuru Staff
Mar 1, 2001
2 minute read
CodeGuru content and product recommendations are editorially independent. We may make money when you click on links to our partners. Learn More

Objects


provide a way to divide a program up into independent sections. Often, you also


need to turn a program into separate, independently-running subtasks.

Each


of these independent subtasks is called a

thread,
and you program as if each thread runs by itself and has the CPU to itself.
Some underlying mechanism is actually dividing up the CPU time for you, but in
general, you don’t have to think about it, which makes programming with
multiple threads a much easier task.

Some


definitions are useful at this point. A

process
is a self-contained running program with its own address space. A
multitasking
operating system is capable of running more than one process (program) at a
time, while making it look like each one is chugging along by periodically
providing CPU cycles to each process. A thread is a single sequential flow of
control within a process. A single process can thus have multiple concurrently
executing threads.

There


are many possible uses for multithreading, but in general, you’ll have


some part of your program tied to a particular event or resource, and you


don’t want to hang up the rest of your program because of that. So you


create a thread associated with that event or resource and let it run


independently of the main program. A good example is a “quit”


button – you don’t want to be forced to poll the quit button in


every piece of code you write in your program and yet you want the quit button


to be responsive, as if you


were

checking it regularly. In fact, one of the most immediately compelling reasons


for multithreading is to produce a responsive user interface.


Contents

|

Prev

|

Next
CodeGuru Logo

CodeGuru covers topics related to Microsoft-related software development, mobile development, database management, and web application programming. In addition to tutorials and how-tos that teach programmers how to code in Microsoft-related languages and frameworks like C# and .Net, we also publish articles on software development tools, the latest in developer news, and advice for project managers. Cloud services such as Microsoft Azure and database options including SQL Server and MSSQL are also frequently covered.

Property of TechnologyAdvice. © 2026 TechnologyAdvice. All Rights Reserved

Advertiser Disclosure: Some of the products that appear on this site are from companies from which TechnologyAdvice receives compensation. This compensation may impact how and where products appear on this site including, for example, the order in which they appear. TechnologyAdvice does not include all companies or all types of products available in the marketplace.