Difference between revisions of "Scheduler Client Assignment"
| (8 intermediate revisions by the same user not shown) | |||
| Line 1: | Line 1: | ||
| + | =Motivation= | ||
| + | We learn the basics of using an instance of Scheduler to leave work behind for other processors via the void_fork method. In this case we will use a simple for loop to get the job done. You will likely need to employ a strategy to get around the finality requirement for lambdas. | ||
| + | |||
| + | =Code To Investigate= | ||
| + | ==interface Scheduler== | ||
| + | The scheduler is intended to be extremely bare bones. It doesn't even have join. It simply has a void_fork method. [https://docs.oracle.com/javase/8/docs/api/java/lang/Runnable.html Runnable] is simply an interface with one and only one abstract method: void run(); This means void_fork is suitable for being called with a lambda. | ||
| + | |||
| + | {{CollapsibleCode|Scheduler|<syntaxhighlight lang="java"> | ||
| + | public interface Scheduler { | ||
| + | void void_fork(Runnable runnable); | ||
| + | } | ||
| + | </syntaxhighlight>}} | ||
| + | |||
| + | ==SchedulerClient main== | ||
| + | SchedulerClient's main method simply creates an implementation of Scheduler (namely ExecutorServiceScheduler) and passes that instance to the output method (which you will implement) along with the number 10. After you have left behind the tasks to print the numbers [0, 10), main will joinAll of the tasks you left behind. | ||
| + | |||
| + | {{CollapsibleCode|SchedulerClient main|<syntaxhighlight lang="java"> | ||
| + | public class SchedulerClient { | ||
| + | private static void output(Scheduler scheduler, int N) { | ||
| + | throw new NotYetImplementedException(); | ||
| + | } | ||
| + | |||
| + | public static void main(String[] args) throws InterruptedException, ExecutionException { | ||
| + | ExecutorServiceScheduler scheduler = new ExecutorServiceScheduler(); | ||
| + | output(scheduler, 10); | ||
| + | scheduler.joinAll(); | ||
| + | } | ||
| + | }</syntaxhighlight>}} | ||
| + | |||
=Code To Implement= | =Code To Implement= | ||
{{CodeToImplement|SchedulerClient|output|scheduler.group}} | {{CodeToImplement|SchedulerClient|output|scheduler.group}} | ||
| Line 7: | Line 36: | ||
=Code To Run= | =Code To Run= | ||
| + | {{Client|SchedulerClient|scheduler.grou|main}} | ||
| + | |||
Run SchedulerClient in the scheduler.group package (the file you are editing). | Run SchedulerClient in the scheduler.group package (the file you are editing). | ||
The output should look something like this (not necessarily in this order as it should be parallel). | The output should look something like this (not necessarily in this order as it should be parallel). | ||
| − | + | {{CollapsibleConsole|SchedulerClient output|<pre style="border: 0px; background: #000; color:#fff;"> | |
| + | 0 | ||
3 | 3 | ||
2 | 2 | ||
| Line 19: | Line 51: | ||
7 | 7 | ||
8 | 8 | ||
| − | 9</ | + | 9 |
| + | </pre>}} | ||
Latest revision as of 18:57, 8 May 2025
Contents
Motivation
We learn the basics of using an instance of Scheduler to leave work behind for other processors via the void_fork method. In this case we will use a simple for loop to get the job done. You will likely need to employ a strategy to get around the finality requirement for lambdas.
Code To Investigate
interface Scheduler
The scheduler is intended to be extremely bare bones. It doesn't even have join. It simply has a void_fork method. Runnable is simply an interface with one and only one abstract method: void run(); This means void_fork is suitable for being called with a lambda.
| Scheduler |
|---|
public interface Scheduler {
void void_fork(Runnable runnable);
}
|
SchedulerClient main
SchedulerClient's main method simply creates an implementation of Scheduler (namely ExecutorServiceScheduler) and passes that instance to the output method (which you will implement) along with the number 10. After you have left behind the tasks to print the numbers [0, 10), main will joinAll of the tasks you left behind.
| SchedulerClient main |
|---|
public class SchedulerClient {
private static void output(Scheduler scheduler, int N) {
throw new NotYetImplementedException();
}
public static void main(String[] args) throws InterruptedException, ExecutionException {
ExecutorServiceScheduler scheduler = new ExecutorServiceScheduler();
output(scheduler, 10);
scheduler.joinAll();
}
}
|
Code To Implement
| class: | SchedulerClient.java | |
| methods: | output | |
| package: | scheduler.group | |
| source folder: | student/src/main/java |
method: private static void output(Scheduler scheduler, int N)
(parallel implementation required)
Fork N tasks via scheduler.void_fork which print the indices [0..N).
Code To Run
| class: | SchedulerClient.java | CLIENT |
| package: | scheduler.grou | |
| source folder: | student/src/main/java |
Run SchedulerClient in the scheduler.group package (the file you are editing).
The output should look something like this (not necessarily in this order as it should be parallel).
| SchedulerClient output |
|---|
0 3 2 1 4 5 6 7 8 9 |