JavaSE8 Base Multi-threaded anonymous inner class both rewrite the thread in run and implement the run in Runnable

Source: Internet
Author: User
Tags api manual thread class

Li Wu:
Learn to think more, honouring teachers save Thanksgiving. Leaf See root Three return one, living water Qingyuan hidden in zero.
Meekness The Lord of Conscience, Haoyuedangkong the King of Freedom. Willing to give the most ascetic no regrets, sincere advice and the line and cherish.



Os:windows7 x64
Jdk:jdk-8u131-windows-x64
Ide:eclipse Oxygen Release (4.7.0)



Test class:

Package blog.jizuiku6;/** * The anonymous inner class inherits the thread class, overriding the run () method         and implementing the Runnable interface, which implements the run () method *  * @author Jizuiku * @version V17.09.27 */public class Fixedetirdemo {public static void main (string[] args) {new Thread (new Runnable () {@Overridepublic void Run () {//TODO auto-generated method stubfor (int i = 0; i < 5550; i++) {System.out.println ("Bo Guest Park "+ i);}}) {@Overridepublic void Run () {//TODO auto-generated method Stubsuper.run (); for (int i = 0; i <; i++) {SYSTEM.OUT.P Rintln ("____ to the most bitter" + i);}}. Start ();//The result of the experiment is that two run methods are executed! This is not the same as shown in the video tutorial, the video only executes the subclass of Run ()//And as if the execution of two run () method or the order of the//video is JDK7, to the most bitter is jdk8, and this has a relationship? This phenomenon does not understand, to the most bitter advice ...}}


Results:

In response to this result, to the most bitter question, whether the outcome of the order of output is coincidental, or inevitable? which contains a profound philosophy, this time no teacher to lead, can only use their own study to analyze!



The following is the most bitter exploration and analysis process,

First with the anti-compilation look, open the Bin folder of the specified package, to the most bitter see three. class file, fun! The following three source code files are obtained by using Xjad decompile

Decompiled by Jad V1.5.8e2. Copyright 2001 Pavel kouznetsov.//Jad Home page:http://kpdus.tripod.com/jad.html//decompiler options:packimports (3) F Ieldsfirst ANSI Space//Source File Name:   fixedetirdemo.javapackage blog.jizuiku6;import Java.io.PrintStream; public class Fixedetirdemo{public Fixedetirdemo () {}public static void Main (String args[]) {(New Thread (new Runnable () { public void Run () {for (int i = 0; i < 5550; i++) System.out.println ((New StringBuilder ("Blog Park"). Append (i). toString ());}} {public void Run () {Super.run (), for (int i = 0; i < i++) System.out.println ((New StringBuilder ("____ to the most bitter"). Append (i). toString ());}). Start ();}}

Decompiled by Jad V1.5.8e2. Copyright 2001 Pavel kouznetsov.//Jad Home page:http://kpdus.tripod.com/jad.html//decompiler options:packimports (3) F Ieldsfirst ANSI Space//Source File Name:   fixedetirdemo.javapackage blog.jizuiku6;import java.io.printstream;// Referenced classes of package Blog.jizuiku6://fixedetirdemoclass fixedetirdemo$2 extends thread{public void run () { Super.run (); for (int i = 0; i < i++) System.out.println ((New StringBuilder ("____ to most bitter"). Append (i). toString ());} Fixedetirdemo$2 (Runnable $anonymous 0) {super ($anonymous 0);}}

Decompiled by Jad V1.5.8e2. Copyright 2001 Pavel kouznetsov.//Jad Home page:http://kpdus.tripod.com/jad.html//decompiler options:packimports (3) F Ieldsfirst ANSI Space//Source File Name:   fixedetirdemo.javapackage blog.jizuiku6;import java.io.printstream;// Referenced classes of package Blog.jizuiku6://fixedetirdemoclass fixedetirdemo$1implements runnable{public void Run () { for (int i = 0; i < 5550; i++) System.out.println ((New StringBuilder ("Blog Park"). Append (i). toString ());} Fixedetirdemo$1 () {}}

See these three source code, is not each anonymous inner class will be compiled into a separate class? However, even if this feature is understood, it cannot explain the abnormal results of the output. When the most bitter to contemplate, suddenly a flash, with the debug look! So, start debugging.

Step_0

Step_1

Step_2

Step_3

Step_4

Step_5

Step_6

Step_7

Step_8

Step_9

The original cause of the abnormal result is on the Super.run ()! So I'm going to get rid of that, isn't it? As you can tell in the video, only the Run () method in the subclass runs

Code:

Package blog.jizuiku6;/** * The anonymous inner class inherits the thread class, overriding the run () method         and implementing the Runnable interface, which implements the run () method *  * @author Jizuiku * @version V17.09.27 */public class Fixedetirdemo {public static void main (string[] args) {new Thread (new Runnable () {@Overridepublic void Run () {//TODO auto-generated method stubfor (int i = 0; i < 5550; i++) {System.out.println ("Bo Guest Park "+ i);}}) {@Overridepublic void Run () {//TODO auto-generated method stub//Super.run (); for (int i = 0; i <; i++) {System.ou T.println ("____ to the most bitter" + i);}}. Start ();//When the hardest to get rid of Super.run (), found the same as the effect of the video tutorial//When the most bitter again super carefully watch the code in the video tutorial found: The video did not write Super.run ()//After this toss, learned a lot ...// In writing code, every line of code is alive, meaningful, cherished, and treated seriously}.

Results:

Problem analysis completed, the Cloud light wind light ...

Java is good, worth learning.
Learning Resources: API manual + Java source code + Xjad + Pure heart.

JavaSE8 Base Multi-threaded anonymous inner class overrides both the run in thread and the run in runnable

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.