On your Terminal,
Step 1: cd
to the notification_app
folder
Step 2: Run mvn clean package
If you do not have mvn yet, run sdk install maven
Step 3: Open the App.java
file loated in notification_app/src/main/java/sg/edu/ntu/App.java
Step 4: Right-click anywhere on the code and select "Run Java" (java bundle extensions for vscode is needed)
If you do not want to see those ugly read lines falsely highlighting errors on you VS Code, you are to launch a new instance of VS Code opening the folder
notification_app
. This is a good practice for coding java application. Your IDE should always open the root directory of your project.
- Spawn a new thread with
CompletableFuture
- Use a
Queue
to storeCustomNotification
objects in aNotificationQueue
class - Implement
ExecutorService
onApp.js
to clear the objects stored inQueue
If learners are continuing from their old repository, go to pom.xml to change the following:
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.7</maven.compiler.source> <!-- Change the value from 1.7 to 17 -->
<maven.compiler.target>1.7</maven.compiler.target> <!-- Change the value from 1.7 to 17 -->
</properties>
This tells maven to compile the class to Java 17 runtime, so that we can use lambda expression
which a new feature from Java 8
- Concurrency and Parallelism
From: https://i.stack.imgur.com/V5sMZ.png
- Processes and Threads
A multiple core CPU can run computation on multiple processes. Each process will spawn threads. We do not write Java program to explicitly spawn new thread when we write multithreading code. The decision is on the CPU and OS.
When you launch a MS Word, the program launched lives in a process (and underlying thread). When you launch the second MS Word, two instances of program is used and may live in the same thread concurrently. This does not mean two CPU core is utilized - as the decision is up to the CPU and OS.
-
Five ways to spawn new thread in Java
a. Extends
Thread
b. Implement
Runnable
c. Using the
ExecutorService
classd. Join/Fork
e.
CompetableFuture
-
The preferred Approach
Before Java 8, option (b) and (c) is preferred. Since Java 8, (b) and (e).
- try-catch-finally
- Custom Exception
- Good practice to log exceptions
Optional: CompletableFuture (1st part of next lesson)
- Abstraction
- Polymorphism
Polymorphism - The ability to take on more than one form. (using interface
)
Abstraction - Hide unnecessary details so that the consumer only interface with the necessities. (using abstract class
)
Interface is used to pre-determine the shape of multiple unrelated classes. Abstract class is used to enforce the shape of closely related classes.
When you use interface, you think about universal capability
and type
.
- Universal Capability Examples
- Drivable? Connectable? Payable?
- Type example
- The types of
Drivable
car:SportsCar
,FamilyCar
, etcs.
- The types of
When you use abstract class, you think about inheritance
.
- All children class must have __________
In this lesson, we will:
- Convert CustomNotification.java to an abstract class
- Create a Connectable.java as an interface
- Inheritance
- Encapsulation
OOP is Object-oriented Programming and is a set of principles. The actual practices of OOP takes on different form in different programming languages but they are 80% similar. Therefore, remember the concepts, and google/reference for the implementation.
In this lesson, we will:
- Create
SMSNotification
andEmailNotification
child classes instead of usingChannel type
enum. - Override the
send()
behaviour - Provide additional properties
cc
andbcc
to theEmailNotification
class. - We will also observe
encapsulation
in the same piece of code.
- method with arguments/parameters
- method with return values
- method overloading
- class and new keyword
- private properties and accessor methods
- constructor with default values
- static and non-static
A static method is a class method and belongs to the class itself. This means you do not need an instance in order to use a static method. A non-static method is an instance method and belongs to each object that is generated from the class. - Excerpt from LinkedIn
Instantiation: The new keyword is a Java operator that creates the object. Initialization: The new operator is followed by a call to a constructor, which initializes the new object. - Excerpt from Oracle.com