mode 1 Flashcards
maven, log4j, junit
What is a Thread?
A split in execution. A process. A line of logic. (aka a series of instruction)
How can we create a thread?
There are two ways to create a thread.
It can be created by EXTENDING the Thread class and overriding its run() method: public class Main extends Thread { public void run() { System.out.println("This code is running in a thread"); } }
Another way to create a thread is to implement the Runnable interface: public class Main implements Runnable { public void run() { for(int i =0; i<50; i++) { System.out.println("\t:\t"+Thread.currentThread().getName()); }
}
}
What does the run method do?
The run method acts as the entry point into this thread’s logic JUST like the main method in the main thread
PROS AND CONS OF THE TWO WAYS OF CREATING A CUSTOM THREAD:
Pros of extending:
- -less lines of code to “start” the thread
- -far easier to override the functionality of the Thread class when extending
Pros of implementing:
- -you do NOT use up the one slot you have to extend another class
- -lightweight, meaning you don’t have to include unwanted methods/baggage
What does the Start Method do?
The START method has special logic that will create a separate process (thread) THEN inside that separate thread it will invoke your run() method
What is a subprocess?
A child process that occupies the same heap as the parent process.
>In java, all threads are ACTUALLY subprocesses.
»This means that threads in Java have the same heap, but different stacks
What is multithreading?
It’s when multiple processes run concurrently (at the same time)
In Java, What are the states of a thread?
- New: the thread is new (just created)
- Runnable: it’s able to have processor time (not sure if CURRENTLY running though)
- Waiting: functionality is paused until a specific event has happened (waiting for a “resume” signal)
- Timed Waiting: functionality is paused until a certain time has passed
- Blocked: it can’t continue its process because another process has hogged/claimed a necessary resource (synchronized methods)
- Terminated: terminated aka completed
What does the synchronized keyword do in Java?
The synchronized keyword goes on a method
>For a synchronized method two threads can NOT access THAT SPECIFIC method concurrently
>THAT specific method inside of THAT SPECIFIC object.
>that is to say, that is method1() is synchronized then ThreadOne and ThreadTwo cannot access method1() inside of ObjectOne
>HOWEVER, ThreadOne can access method1() insode of ObjectOne AT THE SAME TIME as ThreadTwo accessing method1() inside of ObjectTwo
What is a marker interface, functional interface?
A marker interface is an interface that has NO abstract methods that you need to implement (marker interfaces allow you to implement black/whitelist scenario in OOP). Serializable is a marker interface.
A functional interface is an interface that has EXACTLY ONE method for you to implement
If an interface has two or more methods for you to implement then it doesn’t have a special name
, its just an interface
What is the different between a white list and a black list?
- in a blacklist by default EVERYONE is allowed access, those on the blacklist have had their access revoked
- in a whitelist by default NO ONE is allowed access, those on the whitelist have been granted access
Serializable happens to be a whitelist scenario
What does the transient keyword do?
the “transient” keyword stops a field from being written to a file
What is Reflection
Reflection allows one to view an object or primitive during runtime of the application. YOu may
also modify the object’s structure/state during runtime. It’s VERY powerful.
What are Streams
Streams are essentially a tool that helps you manipulate and perform calculations upon a Collection or Array using a functional programming approach. (a bit of sugar syntax)
What is the filter Stream Method
Filter is going to LOOK AT a collection then create a SECONDARY collection that contains ONLY the elements that pass a given conditio