Java threads without join — what is printed by main when two worker threads update shared buffers?\n\nclass Test116 \n{ \n static final StringBuffer sb1 = new StringBuffer(); \n static final StringBuffer sb2 = new StringBuffer(); \n public static void main(String args[]) \n { \n new Thread() \n { \n public void run() \n { \n synchronized (sb1) \n { \n sb1.append("A"); \n sb2.append("B"); \n } \n } \n }.start(); \n\n new Thread() \n { \n public void run() \n { \n synchronized (sb1) \n { \n sb1.append("C"); \n sb2.append("D"); \n } \n } \n }.start(); // Line 28\n\n System.out.println(sb1 + " " + sb2); \n } \n}\n\nNo join or additional synchronization is used before the println.

Difficulty: Medium

Correct Answer: Cannot be determined.

Explanation:


Introduction / Context:
This problem explores thread scheduling and visibility when the main thread starts worker threads and immediately prints shared data without waiting. It tests understanding of start(), synchronized blocks, and the absence of join() or other happens-before relationships connecting worker completion to the println call.



Given Data / Assumptions:

  • Two anonymous threads each synchronize on sb1, then append to sb1 and sb2.
  • Main thread starts both and immediately executes System.out.println(sb1 + " " + sb2).
  • No join(), latch, or other coordination is performed.
  • StringBuffer methods are synchronized but that does not force main to wait.


Concept / Approach:
Thread.start() returns promptly; it does not block until run() completes. Without a join or coordination primitive, main may print before any thread runs, after one runs, after both run, or interleaved with them. The println converts sb1 and sb2 to strings at that instant, showing whatever content has been appended so far. Because each worker locks sb1 before modifying both buffers, there is no data race between workers, but there is also no ordering with main.



Step-by-Step Solution:

Case 1: Scheduler runs main immediately → output: "" (empty) + space + "" (empty).Case 2: One worker completes first → output could be "A" or "C" for sb1 and "B" or "D" for sb2.Case 3: Both complete before println → output "AC BD" (or "CA DB") depending on order.Because all are possible, a single deterministic answer cannot be given.


Verification / Alternative check:
Insert t1.join(); t2.join(); before println to force deterministic output showing both letters in each buffer. Without joins, results vary per run and platform.



Why Other Options Are Wrong:

  • "main() will finish before starting threads." is incorrect; start() definitely runs before println, but workers may or may not run before the print.
  • "main() will finish in the middle/after one thread" assert specific schedules not guaranteed.


Common Pitfalls:
Assuming that synchronized modifications imply visibility to unrelated threads without an ordering action like join(), volatile, or synchronized around the read.



Final Answer:
Cannot be determined.

More Questions from Threads

Discussion & Comments

No comments yet. Be the first to comment!
Join Discussion