Java Tutorials - Herong's Tutorial Examples - v8.22, by Herong Yang
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) HerongYang.com. All Rights Reserved. */ import java.util.*; class WeekDayCounter extends Thread { private static final int s_year = 2000; // starting year private static int y_maxi = 10000; // maximum number of years private static int t_maxi = 50; // maximum number of threads private static int t_last = -1; // index of the last thread private static int n_loop = 50; // repeat the work n times private static int[] t_year = new int[t_maxi]; private static boolean[] t_done = new boolean[t_maxi]; private static int[] t_logs = new int[t_maxi]; // years calculated private static int[] y_days = new int[y_maxi]; // weekdays private int t_indx; // index of this thread public static void main(String[] arg) { if (arg.length>0) y_maxi = Integer.parseInt(arg[0]); if (arg.length>1) t_maxi = Integer.parseInt(arg[1]); for (int i=0; i<y_maxi; i++) { y_days[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("Thread #, Years"); int sum = 0; for (int i=0; i<t_maxi; i++) { sum += t_logs[i]; System.out.println(i+", "+t_logs[i]); } System.out.println("Total # of years = "+sum); } public WeekDayCounter() { t_last++; t_indx = t_last; t_done[t_indx] = false; t_year[t_indx] = 0; t_logs[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; int n = 0; for (int i=0; i<n_loop; i++) { c.clear(); c.set(y,0,1); // first day of the year 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; t_logs[t_indx] += 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 initializing y_days with 0 for all the years at the beginning of the program, and setting y_days of a given year to the calculated result of that year. If another thread is running behind and reaches this 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.
In order to slow down the process on fast computers, n_loop = 50 is introduced to repeat the calculation 50 times.
The program is also designed to verify if a program can have more work done with more threads.
See the next sections for test results of this program.
Table of Contents
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
Java Modules - Java Package Aggregation
►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
Assert Statements and -ea" Option