Java Reference
In-Depth Information
total += data [ i ];
}
return
return total ;
} else
else {
// Divide and Conquer
int
int split = length / 2 ;
RecursiveTaskDemo t1 =
new
new RecursiveTaskDemo ( data , start ,
split );
t1 . fork ();
RecursiveTaskDemo t2 =
new
new RecursiveTaskDemo ( data , start + split , length - split );
return
return t2 . compute () + t1 . join ();
}
}
}
The biggest undefined part there is “small enough”; you may have to do some experimenta-
tion to see what works well as a “chunk size.” Or, better yet, write more code using a feed-
back control system, measuring the system throughput as the parameter is dynamically
tweaked up and down, and have the system automatically arrive at the optimal value for that
particular computer system and runtime. This is left as an extended exercise for the reader.
Background Saving in an Editor
Problem
You need to save the user's work periodically in an interactive program.
Solution
Use a background thread.
Discussion
This code fragment creates a new thread to handle background saves, as in most word pro-
cessors:
public
public class
extends Thread {
/** The FileSave interface is implemented by the main class. */
protected
class AutoSave
AutoSave extends
protected FileSaver model ;
Search WWH ::




Custom Search