Java Supports Multi-Threading Programming

This section provides a quick demonstration of the multi-threading programming support in Java language.

Java supports multithreading (concurrency) programming. Multiple threads of executions can be initiated and synchronized in a single Java application. All threads in a single application share the same set of resources.

What Is Multi-Threading Programming? Multi-treading programming is the ability of writing a single application that can execute multiple sections of codes simultaneously.

Here is a sample Java program showing you two threads are executed simultaneously:

/* ThreadDemo.java
 * Copyright (c) 2003 HerongYang.com. All Rights Reserved.
 */
import java.util.*;
class ThreadDemo {
   public static void main(String[] args) {
      Thread wordGenerator = new WordThread();
      wordGenerator.start();

      Thread numberGenerator = new NumberThread();
      numberGenerator.start();
   }

   static class WordThread extends Thread {
      public void run() {
         while (true) {
            Random generator = new Random();
            int size = 3 + generator.nextInt(5);
            String value = "";
            for (int i=0; i<size; i++) {
              int d = generator.nextInt(26) + 'a';
              value += (char) d;
            }
            System.out.println("Word Thread: "+value);
            try {
               sleep(1000);
            } catch (InterruptedException e) {
            }
         }
      }
   }

   static class NumberThread extends Thread {
      public void run() {
         while (true) {
            Random generator = new Random();
            int size = 3 + generator.nextInt(5);
            long value = 0;
            for (int i=0; i<size; i++) {
              int d = generator.nextInt(10);
              value = value*10 + d;
            }
            System.out.println("Number Thread: "+value);
            try {
               sleep(1000);
            } catch (InterruptedException e) {
            }
         }
      }
   }
}

If you run this Java sample program, you will get output similar to this:

java ThreadDemo

Word Thread: cjsdlen
Number Thread: 64845
Word Thread: ldyb
Number Thread: 92076
Word Thread: gari
Number Thread: 114
Word Thread: mjjieu
Number Thread: 2876
Word Thread: xfsrq
Number Thread: 66858
Word Thread: cfs
Number Thread: 35258
Word Thread: urpfi
Number Thread: 4120008

Table of Contents

 About This Book

 2002 - .NET Framework Developed by Microsoft

 1995 - PHP: Hypertext Preprocessor Created by Rasmus Lerdorf

1995 - Java Language Developed by Sun Microsystems

 What Is Java Language

 Java Compilation and Execution Processes

 Java Is Platform Independent

 Java Is an Object-Oriented Language

 Java Supports Automatic Garbage Collection

Java Supports Multi-Threading Programming

 1991 - WWW (World Wide Web) Developed by Tim Berners-Lee

 1991 - Gopher Protocol Created by a University of Minnesota Team

 1984 - X Window System Developed a MIT Team

 1984 - Macintosh Developed by Apple Inc.

 1983 - "Sendmail" Mail Transfer Agent Developed by Eric Allman

 1979 - The Tcsh (TENEX C Shell) Developed by Ken Greer

 1978 - Bash (Bourne-Again Shell) Developed by Brian Fox

 1978 - The C Shell Developed by Bill Joy

 1977 - The Bourne Shell Developed by Stephen Bourne

 1977 - Apple II Designed by Steve Jobs and Steve Wozniak

 1976 - vi Text Editor Developed by Bill Joy

 1974 - Internet by Vinton Cerf

 1972 - C Language Developed by Dennis Ritchie

 1971 - FTP Protocol Created by Abhay Bhushan

 1970 - UNIX Operating System Developed by AT&T Bell Labs

 1957 - FORTRAN Language Developed by IBM

 References

 Full Version in PDF/EPUB