Login | Register   
LinkedIn
Google+
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


advertisement
 

An Introduction to Java Thread Programming  : Page 2

Multi-threaded programming is all but essential for complex problem solving and effective user interfaces. Yet, to many programmers, it remains an arcane science. Java makes threaded programming simple due to its portable nature.


advertisement

WEBINAR: On-Demand

Unleash Your DevOps Strategy by Synchronizing Application and Database Changes REGISTER >

Creating a Java Thread
So, how does one actually create a multithreaded program? The following code composes a simple program that will demonstrate the fundamentals.

This simple, non-threaded, class exposes a method that merely loops from 0 to 100, printing "Hello world" on each iteration.


public class PrintHello
{
  public void run ()
  {
    for (int i = 0; i < 100; i++)
      System.out.println ("Hello world");
  }
}
Now I'll use a driver program to create and invoke the PrintHello class above.


public class RunIt
{
  public static void main (String args [])
  {
    PrintHello p = new PrintHello ();
    p.run ();
  }
}
Executing the program displays the message "Hello world" 100 times. Suppose, however, the RunIt class was really very complicated and you wished to display the message as before, but while performing other initializations in the main() method. That is, the "Hello world" message would display while the program continued to work in the background. As written, this is not possible—any instructions after the call to the run() method will not execute until after the run() method terminates. However, by implementing threads, we can solve this problem.

The easiest way to create a new thread is to derive a class from java.lang.Thread. The code below is a modified version of the PrintHello class from above. It exposes a method that loops from 0 to 100, printing "Hello world" on each iteration.



public class PrintHello extends Thread
{
  public void run ()
  {
    for (int i = 0; i < 100; i++)
      System.out.println ("Hello world");
  }
}
This class is not significantly different from the first one, yet it will still compile and run exactly as before. This proves that a Thread class exists, but as yet, the code is not multithreaded.

The second class must now be altered so that it invokes the PrintHello class in a different manner. Rather than call its run() method, RunIt will now call a start() method.


public class RunIt
{
  public static void main (String args [])
  {
    PrintHello p = new PrintHello ();
    p.start ();
  }
}
Again, the program compiles and generates the same output as before. Hence, it may be safely concluded that the Thread class contains a start method that somewhere invokes a method called run(). Under the covers, though, the program does not work the same as before, because a second thread has actually been completed, and had any further instructions occurred in RunIt they would have executed simultaneously.


Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap
Thanks for your registration, follow us on our social networks to keep up-to-date