Java Reference
In-Depth Information
}
fOutput.println (
"
# ones
="
+ fOnes +
"
for i
="
+
fIlo + "
to
"
+ fIhi +
"
&j
="
+ fJlo +
"
to
"
+ fJhi);
} // run
} // class MatHunter
The program
TaskSplitApplet
creates a matrix with a random distribution
of zero and non-zero elements. It then creates four instances of
MatHunter
, one
for each quadrant of the matrix. Each instance works on the same problem but in
a separate, independent section of the matrix.
public class
TaskSplitApplet
extends JApplet
implements Outputable, ActionListener
{
...Build the interface...
public void start ()
{
int[][] imat
=
new int[2000][2000];
for (int i
=
0; i
<
2000; i++)
{
for (int j
=
0; j
<
2000; j++)
{
if (Math.random()
>
0.5) imat[i][j]
=
1;
}
}
MatHunter mh1 = new MatHunter (imat,0,999,0,999,this);
MatHunter mh2 =
new MatHunter (imat,0,999,1000,1999,this);
MatHunter mh3 =
new MatHunter (imat,1000,1999,0,999,this);
MatHunter mh4 =
new MatHunter (imat,1000,1999,1000,1999,this);
Println ("Start:");
mh1.start ();
mh2.start ();
mh3.start ();
mh4.start ();
}
// start
}
// class TaskSplitApplet
Figure 8.4 shows the results of different threads finishing in a different order each
time the
“Go
„
button is pressed.
Search WWH ::
Custom Search