Skip to content

Instantly share code, notes, and snippets.

@novaknole
Last active June 7, 2018 17:15
Show Gist options
  • Save novaknole/4f916ef0f398078004d1d42b15d8e2ae to your computer and use it in GitHub Desktop.
Save novaknole/4f916ef0f398078004d1d42b15d8e2ae to your computer and use it in GitHub Desktop.
Java Useful Notes
1)JVM and Compilation
Compilation and execution of a Java program is two step process. During compilation phase Java compiler
compiles the source code and generates bytecode. This intermediate bytecode is saved in form of a .class file.
In second phase, Java virtual machine (JVM) also called Java interpreter takes the .class as input and generates output
by executing the bytecode.
2)JRE: Java Runtime Environment. It is basically the Java Virtual Machine where your Java programs run on.
It also includes browser plugins for Applet execution.
JDK: It's the full featured Software Development Kit for Java, including JRE, and the compilers and tools
(like JavaDoc, and Java Debugger) to create and compile programs.
Usually, when you only care about running Java programs on your browser or computer you will only install JRE.
It's all you need. On the other hand, if you are planning to do some Java programming, you will also need JDK.
3)Loose-Tight Coupling
http://javaeasy.weebly.com/types-of-coupling.html
http://www.codesuggestions.com/java/tight-coupling-and-loose-coupling-between-java-objects/
4) Dependency Injection
1) IF I HAVE HOUSE AND HOUSE NEEDS DOOR, GADAVCET CONSTRUCTORSHI
IF DOOR NEEDS DOORKNOB, GADAVCET KONSTRUQTORSHI
HOUSESHI SHEMDEG VIYENEBT AM DOORS DA A.SH
2) calke gveqneba HouseFactory classi,romlis build method mtel saxls mtlianad amigebs,
rom ar momiwios wera door ,doorknobis da house-is sheqmna roca damchirdeba 3 xazis wera
5) THREADS.
1)CONCURRENCY,PARALELISM AND LOTS OF MORE https://www.backblaze.com/blog/whats-the-diff-programs-processes-and-threads/
2)Multithreading is a type of execution model that allows multiple threads to exist within the context of a process such that they execute independently but share their process resources.
3) Sleep vs Yield.
Calling sleep will put the current running thread into the blocked state which means processor will stop this thread for milliseconds specified and after that thread will exit the blocked state and processor will know and when processor decides , it will tell that thread to continue
Calling Yield means thread doesn't go into blocked state.let's say thread name is t1. if I write t1.yield(), it tells processor that it doesn't need more time . (it doesn't get blocked).after that scheduler of processor decides if it runs again or use the resources for other things. mokled, Processor decides it might not stop at all and continue running again.
Join()
if i have worker thread that downloads some files from internet and i want my main thread to wait for that worker thread to finish so that i could have downloaded file and do something with it , i type worker.join() in my main thread. what happens is main thread gets blocked until worker thread is done.
Lock
syncrhonized(this) means this object that all 3 threads are using is locked for all of the threads.
so locked by object Pair doesn't mean i can't use this object to get some values that this object has.
the only thing is if i have another function like decr() and I lock it using this, and i have 2 threads for incr()
and 2 threads for decr(), only one thread can enter each function. so when thread1 goes to incr() function, pair
object that is the same for each thread is locked, if scheduler paused this thread1 and gave time to thread2 for decr()
function, it's still locked by pair so can't access it.
class Pair{
public void incr(){
synchronized(this){
a++;
b++;
}
}
}
class thread1 extends thread{
public void run(){
for(int i=0;i<5;i++){
Pair.incr();
}
}
public static void main(){
thread1 = new thread1().start();
thread2 = new thread1().start();
thread3 = new thread1().start();
}
https://www.youtube.com/watch?v=BLuSN_9PSj0&index=9&list=PLtIX-OhzWn3GTjJIJLtS-FZXkL0faHOGs 1:00:00
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment