☕️JAVA Language Community
2.91K subscribers
144 photos
7 videos
31 files
42 links
☕️ Software, IT, Java, news
💻 IT highlights
🎯 AI update
🖥⌨️🖱
Download Telegram
#Java_Interview_Question

More details:

👉Advantages of Java Multithreading

1️⃣It doesn't block the user because threads are independent and you can perform multiple operations at same time.

2️⃣You can perform many operations together so it saves time.

3️⃣Threads are independent so it doesn't affect other threads if exception occur in a single thread.

@javaCode☕️
#Java_Interview_Question

👉Multitasking

Multitasking is a process of executing multiple tasks simultaneously. We use multitasking to utilize the CPU. Multitasking can be achieved by two ways:

▪️Process-based Multitasking(Multiprocessing)
▪️Thread-based Multitasking(Multithreading)

1️⃣Process-based Multitasking (Multiprocessing)

Each process have its own address in memory i.e. each process allocates separate memory area.
Process is heavyweight.
Cost of communication between the process is high.
Switching from one process to another require some time for saving and loading registers, memory maps, updating lists etc.

2️⃣Thread-based Multitasking (Multithreading)

Threads share the same address space.
Thread is lightweight.
Cost of communication between the thread is low.

@javaCode☕️
Thread is executed inside the process. There is context-switching between the threads. There can be multiple processes inside the OS and one process can have multiple threads.

@javaCode☕️
#Java_Interview_Question

👉Life cycle of a Thread (Thread States)

A thread can be in one of the five states. According to sun, there is only 4 states in thread life cycle in java new, runnable, non-runnable and terminated. There is no running state.

But for better understanding the threads, we are explaining it in the 5 states.

The life cycle of the thread in java is controlled by JVM. The java thread states are as follows:

1️⃣New

The thread is in new state if you create an instance of Thread class but before the invocation of start() method.

2️⃣Runnable

The thread is in runnable state after invocation of start() method, but the thread scheduler has not selected it to be the running thread.

3️⃣Running

The thread is in running state if the thread scheduler has selected it.

4️⃣Non-Runnable (Blocked)

This is the state when the thread is still alive, but is currently not eligible to run.

5️⃣Terminated

A thread is in terminated or dead state when its run() method exits.

@javaCode☕️
👍1
#Design_Patterns
#Creational_patterns

👉Creational patterns

In software engineering, creational design patterns are design patterns that deal with object creation mechanisms, trying to create objects in a manner suitable to the situation. The basic form of object creation could result in design problems or added complexity to the design. Creational design patterns solve this problem by somehow controlling this object creation.


1️⃣ #Abstract_Factory_Design_Pattern

▪️Creates an instance of several families of classes

2️⃣ #Builder_Design_Pattern

▪️Separates object construction from its representation
3️⃣ #Factory_Method_Design_Pattern

▪️Creates an instance of several derived classes

4️⃣ #Object_Pool_Design_Pattern

▪️Avoid expensive acquisition and release of resources by recycling objects that are no longer in use

5️⃣ #Prototype_Design_Pattern

▪️A fully initialized instance to be copied or cloned

6️⃣ #Singleton_Design_Pattern

▪️A class of which only a single instance can exist

@javaCode☕️
☕️JAVA Language Community
#Design_Patterns #Creational_patterns 👉Creational patterns In software engineering, creational design patterns are design patterns that deal with object creation mechanisms, trying to create objects in a manner suitable to the situation. The basic form…
👉Rules of thumb

1️⃣Sometimes creational patterns are competitors: there are cases when either Prototype or Abstract Factory could be used profitably. At other times they are complementary: Abstract Factory might store a set of Prototypes from which to clone and return product objects, Builder can use one of the other patterns to implement which components get built. Abstract Factory, Builder, and Prototype can use Singleton in their implementation.

2️⃣Abstract Factory, Builder, and Prototype define a factory object that's responsible for knowing and creating the class of product objects, and make it a parameter of the system. Abstract Factory has the factory object producing objects of several classes. Builder has the factory object building a complex product incrementally using a correspondingly complex protocol. Prototype has the factory object (aka prototype) building a product by copying a prototype object.

@javaCode☕️
#Java_Interview_Question

👉How to create thread

There are two ways to create a thread:

1️⃣By extending Thread class:

Thread class provide constructors and methods to create and perform operations on a thread.Thread class extends Object class and implements Runnable interface.

2️⃣By implementing Runnable interface:

The Runnable interface should be implemented by any class whose instances are intended to be executed by a thread. Runnable interface have only one method named run().
public void run(): is used to perform action for a thread.


@javaCode☕️
#Java_Interview_Question

👉Thread Scheduler in Java

▪️Thread scheduler in java is the part of the JVM that decides which thread should run.

▪️There is no guarantee that which runnable thread will be chosen to run by the thread scheduler.

▪️Only one thread at a time can run in a single process.

▪️The thread scheduler mainly uses preemptive or time slicing scheduling to schedule the threads.

-----------------------------
👉Difference between preemptive scheduling and time slicing

▪️Under preemptive scheduling, the highest priority task executes until it enters the waiting or dead states or a higher priority task comes into existence.

▪️Under time slicing, a task executes for a predefined slice of time and then reenters the pool of ready tasks. The scheduler then determines which task should execute next, based on priority and other factors.

@javaCode☕️
☕️JAVA Language Community
👉Rules of thumb 1️⃣Sometimes creational patterns are competitors: there are cases when either Prototype or Abstract Factory could be used profitably. At other times they are complementary: Abstract Factory might store a set of Prototypes from which to clone…
3️⃣Abstract Factory classes are often implemented with Factory Methods, but they can also be implemented using Prototype.

4️⃣Abstract Factory can be used as an alternative to Facade to hide platform-specific classes.

5️⃣Builder focuses on constructing a complex object step by step.Abstract Factory emphasizes a family of product objects (either simple or complex). Builder returns the product as a final step, but as far as the Abstract Factory is concerned, the product gets returned immediately.

@javaCode☕️
☕️JAVA Language Community
3️⃣Abstract Factory classes are often implemented with Factory Methods, but they can also be implemented using Prototype. 4️⃣Abstract Factory can be used as an alternative to Facade to hide platform-specific classes. 5️⃣Builder focuses on constructing a…
6️⃣Builder is to creation as Strategy is to algorithm.

7️⃣Builder often builds a Composite.

8️⃣Factory Methods are usually called within Template methods.

9️⃣Factory Method: creation through inheritance. Prototype: creation through delegation.

🔟Often, designs start out using Factory Method (less complicated, more customizable, subclasses proliferate) and evolve toward Abstract Factory, Prototype, or Builder (more flexible, more complex) as the designer discovers where more flexibility is needed.

1️⃣1️⃣Prototype doesn't require subclassing, but it does require an Initialize operation. Factory Method requires subclassing, but doesn't require Initialize.

1️⃣2️⃣Designs that make heavy use of the Composite and Decorator patterns often can benefit from Prototype as well.

@javaCode☕️
#Java_Interview_Question

101) Can we start a thread twice

No. After starting a thread, it can never be started again. If you does so, an IllegalThreadStateException is thrown. In such case, thread will run once but for second time, it will throw exception.

@javaCode☕️
#Java_Interview_Question

102) What if we call run() method directly instead start() method?

Each thread starts in a separate call stack.
Invoking the run() method from main thread, the run() method goes onto the current call stack rather than at the beginning of a new call stack.

@javaCode☕️
#Design_Patterns

#Structural_patterns

▪️In Software Engineering, Structural Design Patterns are Design Patterns that ease the design by identifying a simple way to realize relationships between entities.


1️⃣ #Adapter_Design_Pattern
Match interfaces of different classes

2️⃣ #Bridge_Design_Pattern
Separates an object's interface from its implementation

3️⃣ #Composite_Design_Pattern
A tree structure of simple and composite objects

4️⃣ #Decorator_Design_Pattern
Add responsibilities to objects dynamically

5️⃣ #Facade_Design_Pattern
A single class that represents an entire subsystem

6️⃣ #Flyweight_Design_Pattern
A fine-grained instance used for efficient sharing

7️⃣ #PrivateClassData_Design_Pattern
Restricts accessor/mutator access

8️⃣ #Proxy_Design_Pattern
An object representing another object

@javaCode☕️
#Java_Interview_Question

👉Daemon Thread in Java

Daemon thread in java is a service provider thread that provides services to the user thread. Its life depend on the mercy of user threads i.e. when all the user threads dies, JVM terminates this thread automatically.

There are many java daemon threads running automatically e.g. gc, finalizer etc.

You can see all the detail by typing the jconsole in the command prompt. The jconsole tool provides information about the loaded classes, memory usage, running threads etc.

👉Points to remember for Daemon Thread in Java

▪️It provides services to user threads for background supporting tasks. It has no role in life than to serve user threads.

▪️Its life depends on user threads.

▪️It is a low priority thread.

@javaCode☕️
#Java_Interview_Question

103) Why JVM terminates the daemon thread if there is no user thread?

The sole purpose of the daemon thread is that it provides services to user thread for background supporting task. If there is no user thread, why should JVM keep running this thread. That is why JVM terminates the daemon thread if there is no user thread.

@javaCode☕️
❗️Note: If you want to make a user thread as Daemon, it must not be started otherwise it will throw IllegalThreadStateException.

@javaCode☕️
#Design_Patterns
#Adapter_Design_Pattern

👉Intent

Convert the interface of a class into another interface clients expect. Adapter lets classes work together that couldn't otherwise because of incompatible interfaces.

▪️Wrap an existing class with a new interface.

▪️Impedance match an old component to a new system

@javaCode☕️
☕️JAVA Language Community
#Design_Patterns #Adapter_Design_Pattern 👉Intent Convert the interface of a class into another interface clients expect. Adapter lets classes work together that couldn't otherwise because of incompatible interfaces. ▪️Wrap an existing class with a new…
#Adapter_Design_Pattern

👉Problem

An "off the shelf" component offers compelling functionality that you would like to reuse, but its "view of the world" is not compatible with the philosophy and architecture of the system currently being developed.

@javaCode☕️
#Java_Interview_Question

👉Java Thread Pool

Java Thread pool represents a group of worker threads that are waiting for the job and reuse many times.

In case of thread pool, a group of fixed size threads are created. A thread from the thread pool is pulled out and assigned a job by the service provider. After completion of the job, thread is contained in the thread pool again.

👉Advantage of Java Thread Pool

Better performance It saves time because there is no need to create new thread.

@javaCode☕️