Difference between revisions of "Lambda Demos"

From CSE231 Wiki
Jump to navigation Jump to search
Line 7: Line 7:
 
=Code To Investigate=
 
=Code To Investigate=
 
==async,finish==
 
==async,finish==
 +
<nowiki> launchApp(() -> {
 +
System.out.println("start");
 +
 +
finish(() -> {
 +
async(() -> {
 +
sleepRandom(1_000);
 +
System.out.println("-apples");
 +
});
 +
async(() -> {
 +
sleepRandom(1_000);
 +
System.out.println("--oranges");
 +
});
 +
async(() -> {
 +
sleepRandom(1_000);
 +
System.out.println("---bananas");
 +
});
 +
async(() -> {
 +
sleepRandom(1_000);
 +
System.out.println("----mangoes");
 +
});
 +
});
 +
 +
System.out.println("stop");
 +
});</nowiki>
 +
 
{{CodeToImplement|LambdasAsyncAndFinishExample|main|lambda.demo.asyncfinish}}
 
{{CodeToImplement|LambdasAsyncAndFinishExample|main|lambda.demo.asyncfinish}}
  
Line 12: Line 37:
  
 
{{CodeToImplement|NamedClassesAsyncAndFinishExample|main|lambda.demo.asyncfinish}}
 
{{CodeToImplement|NamedClassesAsyncAndFinishExample|main|lambda.demo.asyncfinish}}
 +
 
==compute==
 
==compute==
 
{{CodeToImplement|LambdasMapComputeExample|main|lambda.demo.compute}}
 
{{CodeToImplement|LambdasMapComputeExample|main|lambda.demo.compute}}

Revision as of 18:47, 28 February 2018

Motivation

JDK 8 added lambdas to alleviate the bulkiness of anonymous inner classes. We use lambdas heavily in 231.

Background

Lambda Expressions

Code To Investigate

async,finish

		launchApp(() -> {
			System.out.println("start");

			finish(() -> {
				async(() -> {
					sleepRandom(1_000);
					System.out.println("-apples");
				});
				async(() -> {
					sleepRandom(1_000);
					System.out.println("--oranges");
				});
				async(() -> {
					sleepRandom(1_000);
					System.out.println("---bananas");
				});
				async(() -> {
					sleepRandom(1_000);
					System.out.println("----mangoes");
				});
			});

			System.out.println("stop");
		});
class: LambdasAsyncAndFinishExample.java Java.png
methods: main
package: lambda.demo.asyncfinish
source folder: student/src/main/java
class: AnonymousInnerClassesAsyncAndFinishExample.java Java.png
methods: main
package: lambda.demo.asyncfinish
source folder: student/src/main/java
class: NamedClassesAsyncAndFinishExample.java Java.png
methods: main
package: lambda.demo.asyncfinish
source folder: student/src/main/java

compute

class: LambdasMapComputeExample.java Java.png
methods: main
package: lambda.demo.compute
source folder: student/src/main/java
class: AnonymousInnerClassesMapComputeExample.java Java.png
methods: main
package: lambda.demo.compute
source folder: student/src/main/java
class: NamedClassesMapComputeExample.java Java.png
methods: main
package: lambda.demo.compute
source folder: student/src/main/java

user interface

class: FxLambdaDemoApp.java VIZ
package: lambda.demo.viz
source folder: student/src//java