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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T18:42:45+00:00 2026-06-01T18:42:45+00:00

I have a class with event method that would often be called. Let’s say

  • 0

I have a class with event method that would often be called. Let’s say the method name is: onClicked. Inside the method, I would create a thread to do some stuffs.

First, I tried by creating a thread object inside the event method. The code is like this:

public class MyService {

    //Event method
    public void onClick() {
        new Thread(new Runnable() {

            @Override
            public void run() {
                // do some stuff here
            }
        }).start();
    }
}

But since the onClick would often be called, I’m worried if the new thread object who created everytime when the event method called would create memory-space or performance issue?

Alternatively, I can create a class member thread solution like this:

public class MyService {
    private Thread myThread;

    //Event method
    public void onClick() {
        myThread = new Thread(new Runnable() {

            @Override
            public void run() {
                  // do some stuff here
            }
        });
        myThread.start();
    }
}

So which code is better? 1st or 2nd? Or is there other better solution?

  • 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-01T18:42:46+00:00Added an answer on June 1, 2026 at 6:42 pm

    You can’t use the second code without reinitializing your Thread on each OnClick(). This is because once a Thread has finished running, it is “dead.” Thus, subsequent calls to start() will fail and result in an IllegalThreadStateException.

    It would be better to use the first code, as the GC will remove the Thread after it has completed.

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

Sidebar

Related Questions

I have a class called EventConsumer which defines an event EventConsumed and a method
Let's say I have defined the following class: public abstract class Event { public
I have a static class that I would like to raise an event as
I have following event class. I have a question related to the Property method
I have a class that has two method in it, one calls a class
I have a method in a class that performs several tasks (calling other methods,
I would like to know: Subscriber is a name for class that subscribes for
I have several lines of a method that I would like to ensure that
I have a Django model called Event that has a date field for the
I have this code : class Event{}; class CustomEvent:public Event{}; class Handler { public:

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.