Threads Computing in Java – Part 1

Threads:

  • A simplistic view of a computer is that it has a CPU that performs computations, memory that contains the program that the CPU executes, and memory that holds the data on which the program operates.
  • In this view, there is only one job performed. A more complete view of most modern computer system provides for the possibility of performing more than one job at the same time.
  • You do not need to be concerned with how multiple job performance is achieved, just consider the implications from a programming point of view.
  • Performing more than one job is similar to having more than one computer.
  • In this module, a thread, or execution context, is considered to be the encapsulation of a virtual CPU with its own program code and data.
  • The class java.lang.Thread enables you to create and control threads.

Note: This module uses the term Thread when referring to the class java.lang.Thread and thread when referring to an execution context.

A thread, or execution context, is composed of three main parts:

  • A virtual CPU
  • The code that the CPU executes
  • The data on which the code works

A process is a program in execution.

One or more threads do constitute a process.

A thread is composed of the CPU, the code, and the data.

Code can be shared by a multiple threads, independent of data.

Two threads do share the same code when they execute code from instances of the same class.

Likewise data can be shared by multiple threads, independent of code.

Two threads do share the same data when they share access to common object.

In JAVA programming language, the virtual CPU is encapsulated in an instance of the Thread class. When a thread is constructed, the code and the data that define its context are specified by the object passed to its constructor.

Threads Computing in Java

Creating the Thread:

  • In this, it examines how you create a  thread, and how you use constructor arguments to supply teh code and the data for a thread when it runs.
  • A Thread constructor takes an arguments that is an instance of Runnable.
  • An instance of the Runnable is made from a class that implements the Runnable interface (that is, it provides a public void run() method).

For example:

  1. public class ThreadTester
  2. {
  3. public static void main(String args[])
  4. {
  5. HelloRunner r = new HelloRunner();
  6. Thread t = new Thread(r);
  7. t.start();
  8. }
  9. }
  10. class HelloRunner implements Runnable
  11. {
  12. int i;
  13. public void run()
  14. {
  15. i=0;
  16. while(true)
  17. {
  18. System.out.println(“Hello ” + i++);
  19. if(i==50)
  20. {
  21. break;
  22. }
  23. }
  24. }
  25. }
  • First, the main method constructs an instance r of class HelloRunner.
  • Instance r has its own data, in this case the integer i. Because the instance, r, is passed to the Thread class constructor, r’s integer i is the data with the thread works when it runs.
  • The thread always beings executing at the run method of its loaded Runnable instance (r in this example).
  • A multithreaded programming environment enables you to create multiple threads based on the same Runnable interface.
  • You can do this as follows:

Thread t1 = new Thread(r);

Thread t2 = new Thread(r);

  • In this case, both threads share the same data and the code.
  • To summarize, a thread is referred to through an instance of a Thread object.
  • The thread begins execution at the start of loaded Runnable instance’s run method.

The data that the thread works on its taken from the specific instance of Runnable, which is passed to that Thread constructor. Now, It is time to stop here, tomorrow we will discuss advance topic about this regarding Thread Scheduling.

Sourabh Bhunje

Sourabh Bhunje, B.E. IT from Pune University. Currently Working at Techliebe. Professional Skills: Programming - Software & Mobile, Web & Graphic Design, Localization, Content Writing, Sub-Titling etc. http://techliebe.com/about-us

Leave a Reply