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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T01:42:04+00:00 2026-06-05T01:42:04+00:00

public class GetCurrentThread implements Runnable { Thread th; public GetCurrentThread(String threadName) { th =

  • 0
public class GetCurrentThread implements Runnable {
   Thread th;

   public GetCurrentThread(String threadName) {
      th = new Thread(this,threadName); //<----DOUBT 
      System.out.println("get threadname "+th.getName());
      th.start();
   }

  public void run() {
     System.out.println(th.getName()+" is starting.....");
     System.out.println("Current thread name : " + Thread.currentThread().getName());
  }

  public static void main(String args[]) {
     System.out.println("Current thread name : " + Thread.currentThread().getName());
     new GetCurrentThread("1st Thread");
     //new GetCurrentThread("2nd Thread");
   }
}

Can someone explain what the 2nd line of above code is doing?
My understanding for “th = new Thread(this,threadName)” is, it will create thread object with the name given; let say name “1st Thread”.Now, What “this” keyword is doing here?because when i remove this and try get name of the thread I got the name with no issues, but it never started run().can someone explain in simple terms rather than with one single line answer. i really appreciate everyone’s help.

  • 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-05T01:42:06+00:00Added an answer on June 5, 2026 at 1:42 am

    th = new Thread(this, threadName); //<—-DOUBT

    Your 2nd line is constructing a new Thread object with your GetCurrentThread class as the target (this) and a thread name of "threadName". Your this can be the target because it implements Runnable. Inside of the Thread class, when the thread is started, it calls Thread.run() which does:

    public void run() {
        if (target != null) {
            target.run();
        }
    }
    

    Your GetCurrentThread is the target since you pass this in to the constructor. Really it should not be called GetCurrentThread since it isn’t a thread. Two lines after you have constructed your Thread, you start it running:

    th.start();

    The start() method does the actual work of creating a separate working native thread. The first thing the thread does is to call the run() method in your GetCurrentThread class.

    As a comment, typically it is not recommended for a class to start() itself in its constructor. There are race conditions that are inherent with this that can cause problems. It is better to have a start() method on your GetCurrentThread

    /** start the underlying thread */
    public void start() {
       th.start();
    }
    

    Your main would look like:

    public static void main(String args[]) {
       System.out.println("Current thread name : " + Thread.currentThread().getName());
       GetCurrentThread thread1 = new GetCurrentThread("1st Thread");
       thread1.start();
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

public class saveButtonListener implements ActionListener{ public void actionPerformed(ActionEvent ev){ JFileChooser chooser= new JFileChooser(); String
public class IdAsync extends AsyncTask<String, Void, Void> { AlertDialog alertDialog = new AlertDialog.Builder(MainClass.this).create(); protected
public class WrapperTest { static { print(10); } static void print(int x) { System.out.println(x);
public class Divers { public static void main(String args[]){ String format = |%1$-10s|%2$-10s|%3$-20s|\n; System.out.format(format,
public class Test{ public void Test(String name){}; public static void main() { Test t=new
public class MCQSample extends Activity implements OnClickListener{ TextView title; String gotBread; RadioGroup AnswerRG; int
public class InterruptedInput { public static void main(String[] args) { InputThread th=new InputThread(); //worker
public class Base{ protected String str; public static final Base ERROR = new Base(error);
public class Buddy { public string[] Sayings; } Buddy one = new Buddy(); one.Sayings
public class Foo { public string Bar {get; set;} } How do I get

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.