Posts with #JavaInterviewQuestion

#JavaInterviewQuestion

Public posts on X that use #JavaInterviewQuestion.

Related hashtags

  1. Makakmayum@makakmayum_sid
    #JavaInterviewQuestion What is ThreadLocal and where do you use it in Java? In my last project we had to save a user's experimental data which needed to be saved and used per request for each user(not valid after a request). We tried using Spring's Request scope however we were using GRPC requests and not HTTP, and Spring scopes are applicable only with HTTP requests. For a quick fix, we used ThreadLocal (later we changed to save data to GRPC contexts using an interceptor). ThreadLocal in Java is a way to create a local variable for each thread. This means each thread has its own independent copy of the variable, and changes made by one thread won't affect the others. It's used to avoid thread-safety issues when multiple threads need to access the same type of object. 🧵 Best Use Case: Managing User Session Information A prime use case for ThreadLocal is managing per-thread information that's needed across different methods without passing it as an argument. A common example is handling user session data or a database connection in a multi-threaded web application. Let's say a user's request is handled by a single thread. The user's ID needs to be accessible by various layers of the application (e.g., authentication, logging, data access). Instead of passing the userId through every method signature, you can store it in a ThreadLocal at the beginning of the request. Analogy to remember the concept: A Hotel Room Key 🏨 →Without ThreadLocal: It's like having one key for the entire hotel. Everyone would have to wait for the key to be free, and it would be a chaotic mess. →With ThreadLocal: It's like each guest (thread) gets their own key to their own room (variable). They can enter and leave whenever they want without interfering with anyone else. Eg. // Store a unique ID for the current thread public static final ThreadLocal<String> requestId = new ThreadLocal<>(); // In a request-handling method: requestId.set(UUID.randomUUID().toString()); // This ID is now accessible anywhere in the current thread's execution.
    0116947
  2. Makakmayum@makakmayum_sid
    #JavaInterviewQuestion Does creation of object with new always happen on the heap? Everyone thinks so. But it might not be, if JVM thinks that it doesn't need to. Let's see an example: class Point { int x, y; } int sum() { Point p = new Point(); p.x = 10; p.y = 20; return p.x + p.y; } Inside sum method, it seems a Point object is created. Here comes JIT compiler, it analyzes the code: •Does p escape this method? •Is it returned, stored, or passed somewhere else? If the answer is no, the JVM cancels the heap allocation. This is called Escape Analysis. It goes further: instead of creating a Point, the JVM replaces it with its fields (int x, int y). This is Scalar Replacement. So the new Point() you see in code never actually exists at runtime. The JVM just works with two integers directly. Why it matters: •No heap allocation → no GC pressure •Faster execution → stack-allocated scalars •Short-lived objects are cheap → the JVM often optimizes them away That’s why the old advice “object creation is expensive” is mostly a myth in modern Java.
    3419980
  3. Show more posts