From Mechanical Calculators to Modern CPUs: Exploring Computer Fundamentals and Java Concurrency
This comprehensive guide traces the evolution of computing from early mechanical calculators to today's CPUs, delving into binary systems, memory hierarchies, synchronization primitives, thread states, lock mechanisms, and Java concurrency intricacies, while providing code examples, diagrams, and interview insights for developers.
Computer History
Early mechanical calculators such as the abacus and Pascal's devices laid the groundwork for modern computing. Blaise Pascal invented the first mechanical calculator, and Gottfried Wilhelm Leibniz later improved it and introduced binary notation, inspired by Chinese yin‑yang concepts. The first electronic computer, ENIAC, appeared in 1946, followed quickly by the transistor in 1947, which enabled rapid advances in computer design.
CPU Principles
A CPU executes instructions fetched from memory via the bus, converting high‑level code into electrical signals. Modern CPUs use multi‑level caches (typically three levels) to bridge the speed gap between the fast ALU and slower main memory. Cache line size, locality of reference, and cache‑coherency protocols are critical for performance.
Thread Basics and States
Java threads transition through several states: NEW, RUNNABLE (including READY and RUNNING), BLOCKED, WAITING, TIMED_WAITING, and TERMINATED. Understanding these states helps diagnose concurrency issues.
class MyThread extends Thread {
@Override
public void run() {
System.out.println("2: " + this.getState());
for (int i = 0; i < 10; i++) {
try { Thread.sleep(100); } catch (InterruptedException e) { e.printStackTrace(); }
System.out.print(i + " ");
}
System.out.println();
}
}Synchronization and Locks
Java provides several synchronization mechanisms: synchronized (a pessimistic lock), volatile (visibility without mutual exclusion), and explicit locks such as ReentrantLock with lockInterruptibly(). The wait() / notify() pair works with the monitor's wait set and entry list, following an "elevator" algorithm that prefers threads already in the wait set.
public void testJoin() {
Thread t1 = new Thread(() -> { /* work */ });
Thread t2 = new Thread(() -> {
try { t1.join(); } catch (InterruptedException e) { e.printStackTrace(); }
/* more work */
});
t1.start();
t2.start();
}Memory Barriers and JVM Implementation
To enforce ordering, the JVM inserts memory barriers (e.g., lfence, sfence, mfence on x86). The volatile keyword generates acquire/release barriers, preventing the compiler and CPU from reordering accesses.
int field_offset = cache->f2_as_index();
if (cache->is_volatile()) {
if (support_IRIW_for_not_multiple_copy_atomic_cpu) {
OrderAccess::fence();
}
}CAS and Atomic Operations
Compare‑and‑swap (CAS) underlies the java.util.concurrent.atomic classes. The JVM implements CAS using the hardware lock cmpxchg instruction, which atomically compares a memory location with an expected value and, if equal, writes a new value.
public final int incrementAndGet() {
for (;;) {
int current = get();
int next = current + 1;
if (compareAndSet(current, next))
return next;
}
}Interview Questions and Practical Examples
Common interview topics covered include the difference between interrupt(), isInterrupted(), and Thread.interrupted(), the effect of synchronized on visibility, lock escalation (biased → lightweight → heavyweight), and why Thread.isAlive() may return true after a thread has finished when the thread still holds the monitor lock.
public static void main(String[] args) {
new Test().startThreadA();
}These examples illustrate why a waiting thread (Thread‑A) is often notified before a competing thread (Thread‑C) due to the JVM's default notification policy that prefers the wait set over the contention queue.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Open Source Linux
Focused on sharing Linux/Unix content, covering fundamentals, system development, network programming, automation/operations, cloud computing, and related professional knowledge.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
