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

  • SEARCH
  • Home
  • 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 8842479
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T10:58:07+00:00 2026-06-14T10:58:07+00:00

I am confused with the snippet from this code: void stopTestThread() { // thread

  • 0

I am confused with the snippet from this code:

void stopTestThread() {

    // thread should cooperatively shutdown on the next iteration, because field is now null
    Thread testThread = m_logTestThread;
    m_logTestThread = null;
    if (testThread != null) {
      testThread.interrupt();
      try {testThread.join();} catch (InterruptedException e) {}
    }
  }

Does that mean testThread and m_logTestThread are different instances but point to the same object in the memory, so they are the same thread?

If so, what the purpose is of if (testThread != null) ?

  • 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-14T10:58:08+00:00Added an answer on June 14, 2026 at 10:58 am

    Does that mean testThread and m_logTestThread are different instances
    but point to the same object in the memory, so they are the same
    thread?

    This is partially true. Actually testThread and m_logTestThread are two different references not instances. And both the reference are pointing to the same Thread object. So, just making a reference m_logTestThread point to null does not make the testThread reference also point to null.

    You can also see it in practice by a simple example: –

    String str = "abc";
    String strCopy = str;  // strCopy now points to "abc"
    str = null;  // Nullify the `str` reference
    
    System.out.println(strCopy.length()); // Will print 3, as strCopy still points to "abc"
    

    So, even if you set one of the reference to null, the other reference still points to the same Thread object. An object is not eligible for Garbage Collection until it has 0 reference pointing to it, or there is a circular reference.

    See this link: – Circular Reference – wiki page to know what exactly is Circular Refeference.

    what the purpose is of “if (testThread != null)” ?

    Its simple. You can infer from the condition that, it is checking whether the testThread reference is pointing to a null object.
    The null check is done so that, you don’t get a NPE inside the if-construct, where you are trying to interrupt the Thread pointed to by that reference. So if that reference is pointing to null, then you don’t have any thread associated with that reference to interrupt.

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

Sidebar

Related Questions

I am a little confused by this snippet of code (presented in the CocoaFundamentals
This is a small snippet of code taken from some of the examples that
I'm confused by this snippet from the book I'm reading. Text strings are put
I am confused as to why the following code snippet would not exit when
Perhaps I'm over-simplifying this by only giving you a small snippet of code (and
The quoted code snippet from the JDK 7 java.util.ArrayList class confuses me. I can't
Taking this snippet from the famous agile web development with rails book (most recent
I am confused ( that is easy enough!). I have a small snippet of
Really confused why the QR output using RcppArmadillo is different than QR output from
Pretty confused right now, while i feel i know a bit about vhosts this

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.