Java Thread Learning-thread.currenttread (). The difference between getName () and This.getname ()

Source: Internet
Author: User

Long time no Java program, because in order to change the current situation, it is intended to spend two days to learn Java thread development and high concurrency.

Thread development uses the thread class, or the Runnable interface, and the thread class implements the Runnable interface.

Let's start with a little dessert, as follows: Create multiple threads and process them at the same time.

Package Firstthreadstudy;public class MyThread extends Thread {public MyThread () {System.out.println ("MyThread CurrentThread (). GetName () = "+thread.currentthread (). GetName ()); System.out.println ("MyThread this.getname=" + this.getname ());} @Overridepublic void Run () {System.out.println ("Run CurrentThread (). GetName () =" +thread.currentthread (). GetName ()); System.out.println ("Run This.getname=" +this.getname ()), int i=2;while (i>0) {System.out.println ("run" + This.getname () + ": i =" + (i--)); try{thread.sleep (1000);} catch (Exception e) {e.printstacktrace ();}}}}


Test class:

Package Firstthreadstudy;public class Test2mythread {public static void main (string[] args) {//TODO auto-generated method Stubmythread boy = new MyThread (), Boy.setname ("Boy"); MyThread girl = new MyThread (); Girl.setname ("Girl"); Thread car = new Thread (girl); Car.setname ("Car"); Boy.start (); Car.start (); System.out.println ("Method End");}}


In addition, special supplementary notes

Thread.CurrentThread (). GetName ()

And

This.getname ()

, the former represents the name of the thread that called the current method, which represents the current thread object name. may not be very well understood, according to the following output interpretation will be better understood.

One of the above output possibilities is as follows (threads are randomly dispatched):

MyThread CurrentThread (). GetName () =mainmythread this.getname=thread-1mythread CurrentThread (). GetName () = Mainmythread This.getname=thread-2run CurrentThread (). GetName () =boyrun this.getname=boymethod endrun CurrentThread ( ). GetName () =carrun boy:i = 2run This.getname=girlrun girl:i = 2run girl:i = 1run boy:i = 1

The girl object and the car object are two objects, because This.getname () is the code in the Girl object, so this is the girl object accessed by this.

Thread.currenttread (). GetName () refers to the thread object that is currently calling the method, not the Gril object, but the car object, and the girl object is just a parameter passed in, and the Run method of the Girl object is the start () of the car object Method triggers the dispatch, not the start () trigger of the girl object. Although Gril is a thread object, it does not start, so it does not have a thread, but rather relies on the car thread to execute it.

Well, I think the above-mentioned nonsense should be explained.

This article is from the "Happy Cup of Tea" blog, please be sure to keep this source http://gugw9handsome.blog.51cto.com/1187812/1784131

Java Thread Learning-thread.currenttread (). Differences between GetName () and This.getname ()

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.