Core Java -101 Flashcards
Can we do array[i] and i could be long type also?
No i must be only int
What is a Runtime class?
Every Java Application has one instance of this , this will allow instance to interface with env it is running
How to obtain the Runtime?
getRuntime
What is addShutdownHook(Thread hook)
Registers a new virtual-machine shutdown hook.
What is exec methods?
So this has many overloads & use to give the commands on the given environment
exit(int status)
Terminates the currently running Java virtual machine by initiating its shutdown sequence.
freeMemory()
Returns the amount of free memory in the Java Virtual Machine.
gc()
Runs the garbage collector.
halt(int status)
Forcibly terminates the currently running Java virtual machine.
loadLibrary(String libname)
Loads the dynamic library with the specified library name.
removeShutdownHook(Thread hook)
De-registers a previously-registered virtual-machine shutdown hook.
totalMemory()
totalMemory()
Returns the total amount of memory in the Java virtual machine.
What the exec method returns?
Process
What is a java.lang.Process class?
The ProcessBuilder.start() and Runtime.exec methods create a native process and return an instance of a subclass of Process that can be used to control the process and obtain information about it. The class Process provides methods for performing input from the process, performing output to the process, waiting for the process to complete, checking the exit status of the process, and destroying (killing) the process.
Does the methods of PRocess works in all env?
Not necessarily all, may fail is some basic env
How can we obtain the I/O of Process?
Process does not have terminal or console, all standard I/O are redirected to the Parent process, and can be obtained with the methods like getInputStream, getErrorStream, getOutputStream,
How the deadlock may happen while getting getInputStream like methods?
As some env support limited buffer size thus reading and writng to same buffer may cause deadlock
Does the subProcess (process instance) do async?
Yes
destroy()
Kills the subprocess.
exitValue()
Returns the exit value for the subprocess.
waitFor()
Causes the current thread to wait, if necessary, until the process represented by this Process object has terminated.
Where do thread executes?
Within a process
How can we execute a thread in a thread class?
Thread thread = new Thread(new Runnable())
thread.start
The start method will call the run method of runnable
Can we use lambda expression to write the THread with runnable IF?
Yes since runnable is an functional IF
Thread thread = new Thread( () -> { /// do the task });