Application Data Shared by Multiple Threads

This section provides a tutorial example on how to share application data by multiple threads. Data sharing allows multiple threads to work together on a single large task.

Threads of the same running program not only share CPU time, they also share the same memory space that allocated to the program. This is different than multiple programs running at the operating system level, where memory space can not be shared by different running programs.

Sharing the same memory space among many threads is a very nice feature. It allows threads working on the same set of data to accomplish a big task together. But it can also be a trouble maker, because the execution speed of each thread can not be managed. There is no way to know which thread is the first one to reach the data, and which one will be the next one.

The following program shows that how multiple threads can work together to calculate how many weekdays in each year for 2000 years starting from year 2000. In this program, an array, y_days, is declared as static, so it can be shared by all the threads to store the calculated result, the number of weekdays of a specific year.

/* WeekDayCounter.java
 - Copyright (c) 2014, HerongYang.com, All Rights Reserved.
 */
import java.util.*;
class WeekDayCounter extends Thread {
   private static final int t_maxi = 5; // maximum number of threads
   private static int t_last = -1; // index of the last thread
   private int t_indx; // index of this thread
   private static int[] t_year = new int[t_maxi]; 
   private static boolean[] t_done = new boolean[t_maxi];
   private static final int y_maxi = 2000; // maximum number of years
   private static final int s_year = 2000; // starting year
   private static int[] y_days = new int[y_maxi]; // weekdays
   private static int[] y_logs = new int[y_maxi]; // tracking the thread
   public static void main(String[] a) {
      for (int i=0; i<y_maxi; i++) {
         y_days[i] = 0;
         y_logs[i] = 0;
      }
      for (int i=0; i<t_maxi; i++) {
         WeekDayCounter t = new WeekDayCounter();
         t.start();
      }
      System.out.print("Thread: ");
      for (int i=0; i<t_maxi; i++) {
         System.out.print(i+" "); 	
      }
      Date t1 = new Date();
      while(true) {
         System.out.print("\n  Year: ");
         boolean done = true;
         for (int i=0; i<t_maxi; i++) {
            System.out.print(t_year[i]+" ");
            done = done && t_done[i];
         }
         if (done) break;
         try {
            sleep(100);
         } catch (InterruptedException e) {
            System.out.println("Interrupted.");
         }
      }
      Date t2 = new Date();
      System.out.println("\nTime = "+(t2.getTime()-t1.getTime()));
      System.out.println("Year, Threads");
      for (int y=s_year; y<s_year+y_maxi; y++) {
         if (y_logs[y-s_year]>1) {
            System.out.println(y+", "+y_logs[y-s_year]);
         }
      }   
   }
   public WeekDayCounter() {
      t_last++;
      t_indx = t_last;
      t_done[t_indx] = false;
      t_year[t_indx] = 0;
   }
   public void run() {
      GregorianCalendar c = new GregorianCalendar();
      for (int y=s_year; y<s_year+y_maxi; y++) {
      	 t_year[t_indx] = y;
         if (y_days[y-s_year]==0) {
            y_days[y-s_year] = -1;
            c.clear();
            c.set(y,0,1); // first day of the year
            int n = 0;
            while (c.get(Calendar.YEAR)<=y) {
               if (c.get(Calendar.DAY_OF_WEEK)==Calendar.MONDAY) n++;
               if (c.get(Calendar.DAY_OF_WEEK)==Calendar.TUESDAY) n++;
               if (c.get(Calendar.DAY_OF_WEEK)==Calendar.WEDNESDAY) n++;
               if (c.get(Calendar.DAY_OF_WEEK)==Calendar.THURSDAY) n++;
               if (c.get(Calendar.DAY_OF_WEEK)==Calendar.FRIDAY) n++;
               c.add(Calendar.DATE,1);
            }
            y_days[y-s_year] = n;
            y_logs[y-s_year] += 1; 
         }
      }
      t_done[t_indx] = true;
   }
}

When threads are launched, they will all start to work on one year at time, beginning with the same starting year, 2000. Without any coordination, they will all work on year 2000, then 2001. This is not what we want. We need to tell each thread to skip the year if another thread has already finished calculation for that year. This is done by initialize y_days with 0 for all the years at the beginning of the program, and setting y_days of a year to the calculated result of that year. If another thread is running behind and reaches this that year later, it checks the value in y_days for that year. If the value is not 0, it will skip that year.

However, just setting y_days to the result of the end of the calculation is not enough. Because calculating the number of weekdays of one year will take some time, and during this period, another thread might still pick up that year and work on that year, because the y_days value is still 0. A simple way to stop this mistake is to set y_days to -1 at the beginning of the calculation.

The program is also design to verify if a program can have more work done with more threads. Here is the results of the program with different number of threads:

See the next sections for test results of this program.

Last update: 2014.

Table of Contents

 About This Book

 Installing JDK 1.8 on Windows

 Execution Process, Entry Point, Input and Output

 Primitive Data Types and Literals

 Bits, Bytes, Bitwise and Shift Operations

 Managing Bit Strings in Byte Arrays

 Reference Data Types and Variables

 StringBuffer - The String Buffer Class

 System Properties and Runtime Object Methods

 Generic Classes and Parameterized Types

 Generic Methods and Type Inference

 Lambda Expressions and Method References

Execution Threads and Multi-Threading Java Programs

 What Are Processes and Threads?

 The "Thread" Class - Creating Thread Objects With Thread Sub Classes

 The "Runnable" Interface - Creating Thread Objects with Runnable Objects

 CPU Execution Time Shared by Multiple Threads

 CPU Execution Time Shared by Multiple Threads - Test Output

Application Data Shared by Multiple Threads

 Application Data Shared by Multiple Threads - Test Results

 interrupt() - Method to Terminate Thread

 ThreadGroup Class and "system" ThreadGroup Tree

 Synchronization Technique and Synchronized Code Blocks

 Deadlock Condition Example Programs

 Garbage Collection and the gc() Method

 Outdated Tutorials

 References

 PDF Printing Version