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 9232827
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T06:24:22+00:00 2026-06-18T06:24:22+00:00

I have a Java ThreadFactory implementation spawning runnable thread subclass objects in my Android

  • 0

I have a Java ThreadFactory implementation spawning runnable thread subclass objects in my Android application. This application requires that all spawned threads are addressable before a certain event fires, and that upon the firing of said event the spawned threads can be made eligible for garbage collection (e.g. reference count of 0). I was thinking that to satisfy the first requirement I would simply maintain an ArrayList of my thread objects, and that works fine. The trouble comes with the second requirement, and has led me to a series of questions regarding Java reference counting:

Question 1: Does simply storing references to the spawned threads in an ArrayList or other container increment the reference count of each thread object? What would the reference count of each thread object be if I never stored them, but rather allowed my factory to create them and let them run without any defined handle as below?

mvThreadSource.newThread(new BackgroundThread(...)).run();

Question 2: Does the below code sample do anything to the actual thread object pointed to by hTempThread other than increment and immediately decrement its reference count?

BackgroundThread hTempThread;
for(int i=0;i<mvThreadsVector.size();i++){
    hTempThread = mvThreadsVector.get(i); //probably increments ref count of thread
    hTempThread = null; //probably just decrements the ref count of thread back to 
                        //previous value
}

Question 3: Assuming the answer to question 2 is ‘no’, what would be an effective way to store threads spawned by a ThreadFactory implementation such that their reference counts can be reduced to 0 on demand? What would be the proper syntax involved in removing these references? Would the below code sample effectively reduce all involved objects’ (mvThreadsVector, tempThreads, each thread object tracked by mvThreadsVector) reference counts to 0? What exactly does clear() do to the reference counts of objects stored in an arraylist, and what does setting an array reference to null do to the reference counts of elements stored inside (if anything)?

Object[] tempThreads = mvThreadsVector.toArray();
mvThreadsVector.clear(); //possible this line is all I need...
mvThreadsVector = null;
for(int i=0;i<tempThreads.length;i++){
    tempThreads[i] = null;
}
tempThreads = null;

Any help with any/all of the above questions would be very much appreciated!

  • 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-18T06:24:23+00:00Added an answer on June 18, 2026 at 6:24 am
    1. If you have your thread running, until it finishes execution it will be kept in memory (or swapped) but not GC. Even if the Thread has no code of yours referencing it but is executing code, it has at least a reference internally.

    2. You cannot even guarantee that it will increment and decrement the reference count. The compiler may optimize it by realizing that the first assignment has no effect (since it is immediately overwritten) and remove it.

    3. The reference count will be reduced but remember that you may have loops in the reference chain, where each element will have 1 reference count but the whole loop is not accessible and a correct GC should realize that and remove it.

    I think the main issue is that you shouldn’t count on the reference count of threads going down to 0; as long as they are running you should assume they have references so you must also have a way of making sure the thread stops its execution.

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

Sidebar

Related Questions

I have Java Map (out of Strings and Ints) objects that I want to
I have Java based Spring MVC application that also uses Spring security. I am
I have Java desktop application that works with CSV files. And i want add
I have java application that sending( HTTP post method ) credit card numbers to
I have Java application and I export it to runnable .jar application. Can I
I have Java code that exactly does this: // Create a Service instance Dispatch<SOAPMessage>
I have java application say xxxx.exe. Which I manually kill that application, the corresponding
I have Java Desktop application that displays some information in a JTable that may
I have Java ME Application that uses HttpConnection API, Here I am easily able
I have Java Desktop application that displays some information in a JTable that contains

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.