Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8065605
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T11:43:18+00:00 2026-06-05T11:43:18+00:00

In the book Java concurrency in practice , there is an example of customized

  • 0

In the book Java concurrency in practice, there is an example of customized thread (See Listing 8.7 in section 8.3.4). I pasted the code below. There is one thing I don’t quite understand. That is, the run() method copies the volatile variable debugLifecycle before using it. And it has a comment Copy debug flag to ensure consistent value throughout. Is there any need to copy the variable here? If yes, why?

public class MyAppThread extends Thread {
    public static final String DEFAULT_NAME = "MyAppThread";
    private static volatile boolean debugLifecycle = false;

    public MyAppThread(Runnable r) {
        this(r, DEFAULT_NAME);
    }

    public MyAppThread(Runnable runnable, String name) {
        super(runnable, name + "-" + created.incrementAndGet());
        setUncaughtExceptionHandler(new Thread.UncaughtExceptionHandler() {
            public void uncaughtException(Thread t,
                                          Throwable e) {
                log.log(Level.SEVERE,
                        "UNCAUGHT in thread " + t.getName(), e);
            }
        });
    }

    public void run() {
        // Question: why copy the volatile variable here?
        // Copy debug flag to ensure consistent value throughout.
        boolean debug = debugLifecycle;
        if (debug) log.log(Level.FINE, "Created " + getName());
        try {
            alive.incrementAndGet();
            super.run();
        } finally {
            alive.decrementAndGet();
            if (debug) log.log(Level.FINE, "Exiting " + getName());
        }
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T11:43:19+00:00Added an answer on June 5, 2026 at 11:43 am

    The volatile keyword generally means that the variable is accessed by multiple threads. So you make a copy of it’s state once. If, while you are running, another thread modifies it, your copy will be unaffected.

    Otherwise it might be the case that the first log.log() gets executed, but not the finally clause’s log.log(). Which could be confusing or incorrect behavior.

    Even if debugLifecycle were not volatile in a few cases it might still be better to use a copy. But volatile is a “red flag” that this variable might change at any time.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Code from the book Java Concurrency in Practice Listing 8.1 Why is the code
In Brian Goetz's book, Java Concurrency in Practice, his example of a Reentrant lock
In Java Concurrency In Practice book (p.156), there's a statement regarding poison pill approach:
From book Java Concurrency in Practice page 26: You can use volatile variables only
I found the book Java Concurrency in Practice to be an excellent guide to
I'm reading the Book Java Concurrency in Practice . In chapter 15, they are
I am reading this book called Java Concurrency in Practice and the author gives
So I am reading this book titled Java Concurrency in Practice and I am
I am reading Java Concurrency in Practice and kind of confused with the thread
In the book Java Servlet Programming , there's an example servlet on page 54

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.