Java Thread Test22 parallel work example
// Ttest22.java
// creates threads from the class Thread22.java
// demonstrates inter-thread communication (message passing)
// compile this file with the command "javac Ttest22.java"
// your path must include the bin directory of the jdk
// run the program with "java Ttest22"
// on some systems
/********************************************************************************/
/* class Ttest22: examples to build on toward bucket sort */
/********************************************************************************/
public class Ttest22 {
public static int sharedarray[] = new int[4];
public static Thread22 tptr[] = new Thread22[4];
/********************************************************************************/
public static void main(String argv[]) {
int i;
initshared();
printarray();
for (i=0;i<2;i++) {
tptr[i] = new Thread22(i,4,2);
}
System.out.println("Ttest22: made threads");
for (i=0;i<2;i++) {
tptr[i].start();
}
System.out.println("Ttest22: started threads");
//Next, wait for all threads to finish - join()
try {
for (i=0;i<2;i++) {
tptr[i].join();
}
} catch(InterruptedException e) {}
printarray();
tptr[0].sayHello();
}
/********************************************************************************/
/* method passcall(int): testing message passing */
/********************************************************************************/
public static void passcall(int id) {
tptr[id].sayHello();
}
/********************************************************************************/
/* method initshared(): initializes an example array */
/********************************************************************************/
private static void initshared() {
sharedarray[0]=4;
sharedarray[1]=1;
sharedarray[2]=3;
sharedarray[3]=2;
}
/********************************************************************************/
/* method printarray(): note that System.out is not thread safe */
/********************************************************************************/
private static void printarray() {
int i;
System.out.print("Main: shared array= ");
for (i=0;i<4;i++) {
System.out.print(" " + sharedarray[i]);
}
System.out.println();
}
}
//$$$$$$$$$$$$$$$$$$$$$ second file $$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
// this is file Thread22.java
// compile with "javac Thread22.java"
//demonstrates interthread communcation
/********************************************************************************/
/* class Thread22: example parallel threads, toward parallel bucket sort */
/********************************************************************************/
class Thread22 extends Thread {
private int subarray[]; //copy for this thread
private int myid; // thread ID
private int numint; //number of integers in big array
private int numt; //number of threads working
private int numtosort; //number of integers in my array
private int startposition; //pos in big shared array
private int i; //counting variable
/********************************************************************************/
/* method Thread22(int, int, int): thread constructor */
/********************************************************************************/
public Thread22(int me, int numitems, int numthreads) {
System.out.println("Hi from thread " + getName());
myid = me;
numint = numitems;
numt = numthreads;
}
/********************************************************************************/
/* method run(): the thread code */
/********************************************************************************/
public void run() {
numtosort = numint/numt;
startposition = 0 + myid*numtosort;
subarray = new int [numtosort];
System.out.println(getName() + " myid=" + myid);
for (i=0;i subarray[1]) {
temp = subarray[0];
subarray[0] = subarray[1];
subarray[1] = temp;
}
}
}