❌ No, not guaranteed, but ✅ it is highly optimized and designed to do so — more than ThreadPoolExecutor.
Let’s break this down 👇
Continue reading❌ No, not guaranteed, but ✅ it is highly optimized and designed to do so — more than ThreadPoolExecutor.
Let’s break this down 👇
Continue readingyes, if you use a ThreadPoolExecutor, it is possible and very likely that your tasks will be executed on different CPU cores, depending on how many threads your executor runs and how many cores are available.
Let’s break it down 🔧👇
Continue reading| Term | Definition |
|---|---|
| Concurrency | Dealing with lots of tasks at once (not necessarily simultaneously) |
| Parallelism | Actually doing lots of tasks at the same time |
Thread safety means:
A piece of code or a data structure behaves correctly when accessed by multiple threads at the same time, without causing:
In simple terms: It works as expected no matter how many threads use it simultaneously.
Continue readingpublic class HappensBeforeExample {
static int data = 0;
static volatile boolean ready = false;
public static void main(String[] args) {
Thread writer = new Thread(() -> {
data = 42; // Step 1
ready = true; // Step 2 — volatile write, gurantees to be in main memory
});
Thread reader = new Thread(() -> {
while (!ready) {
// spin until ready becomes true
}
System.out.println("Data = " + data); // Step 3 — guaranteed to print 42
});
writer.start();
reader.start();
}
}
Continue reading In the context of the Java Memory Model (JMM), main memory refers to the shared memory area where all variables (especially instance fields, static fields, and array elements) reside and are visible to all threads.
✅ It’s the memory that all working memory (thread-local caches) synchronize with.
Continue readingThe Java Memory Model (JMM) defines how threads interact through memory — specifically, how changes to variables made by one thread become visible to others.
It’s part of the Java Language Specification and is enforced by the Java Virtual Machine (JVM) to ensure consistent behavior across all platforms.
Continue reading🚀 While Java’s built-in binary serialization (Serializable) is powerful, you often want to serialize objects to readable and interoperable formats like JSON, XML, or YAML, especially for:
Let’s walk through the popular formats and tools 👇
Continue readingreadObject() MethodThis is the standard way to hook into deserialization:
private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException {
in.defaultReadObject(); // Restore default values
// 💡 Customize here
if (age < 0) age = 0; // Validation
if (nickname == null) nickname = "Anonymous";
}
Continue reading A Singleton ensures that a class has only one instance in the JVM:
public class Singleton implements Serializable {
private static final Singleton INSTANCE = new Singleton();
private Singleton() {}
public static Singleton getInstance() {
return INSTANCE;
}
}
✅ So when you do:
Singleton s1 = Singleton.getInstance();
Singleton s2 = Singleton.getInstance();
System.out.println(s1 == s2); // true ✅
Continue reading