Thread Thread Is a subprocess that follows a separate execution path, different stack frame and executes independently but they share the same process resources. Multithreading is the process of executing one or more threads simultaneously that helps in executing multiple tasks at the same time. Advantages less memory fast efficient Supports multitasking Exception in one thread does not affect the other Thread lifecycle New Runnable Running Non-Runnable Terminated Ways to create a thread? Extending Thread class Implementing Runnable class Thread.start() vs Thread.run() Class java.lang.Thread .start() Creates a new thread and the run() method is executed on the newly created thread. Can’t be invoked more than one time otherwise throws java.lang.IllegalStateException. Interface java.lang.Runnable.run(), No new thread is created and the run() method is executed on the calling thread itself. Multiple invocation is possible Constructors of a thread class Thread() Thread(String
Inner Class vs Sub-Class Inner class is a class which is nested within another class. It can access all variables and methods in the outer class. A sub-class inherits from another class and can access all public and protected methods and fields in the super class. Protected vs Default Protected - allow access within the package and any subclass from outside the package. Default - allow access within the package only Default access specifier for variables and method is package protected i.e variables and class is available to any other class but in the same package,not outside the package. Static To share a method or variable for all objects. cannot override static methods. Even if we try to override static method,we will not get an complitaion error,nor the impact of overriding when running the code. Static methods belong to a class and not to individual objects and are resolved at the time of compilation (not at runtime) Encapsulation Combining properties and methods in a sin