A probe into multithreading operation in Java

Source: Internet
Author: User
Tags sleep function

Problem leads to:

Java, in fact, is still in Android when the problem encountered, after Android 4.0, access to the network must be implemented in the new thread, so you will encounter this problem. Just create a new Java project for the sake of explaining the problem. In the main function, when you create a new object with a custom class, the value of the new object changes. First look at the project directory

The project consists of two files, one is First.java this is the location of the main function, and Second.java is a custom class.

Step1,

Second.java Code

 Public class Second {    int  A;     int b;          Public second ()    {        set ();    }     Private void set () {        //  TODO auto-generated method Stub        a = 1;         = 2;                    }}

First.java Code

public class First {public static void main (String args[]) {Second SE = new second (); System.out.println (se.a+ "&&" +se.b);}}

Obviously, the output is a=1,b=2; it's not controversial.

Step2, now, we are in the Second.java, and then customize an inner class, called Test Bar. The code is as follows:

public class Second {int a;int b;class test{int ta;int TB;} Test mtest[];p ublic second () {set ();}  private void Set () {//TODO auto-generated Method Stuba = 1;b = 2;mtest = new Test[2];mtest[0] = new test (); mtest[1] = new Test (); mtest[0].ta=3;mtest[0].tb=4;mtest[1].ta=5;mtest[1].tb=6;}}

First.java Code

public class First {public static void main (String args[]) {Second SE = new second (); System.out.println (se.a+ "&&" +se.b); System.out.println (se.mtest[1].ta+ "&&" +SE.MTEST[1].TB);}}

This result is also very good to draw, Se.mtest[1].ta = 5,se.mtest[1].tb=6; no problem.

Here are the main points, the above two steps are not related to multi-threading, now we modify the Second.java code, as follows:

public class Second {int a;int b;class test{int ta;int TB;} Test mtest[];p ublic second () {set ();} private void Set () {//TODO auto-generated Method Stuba = 1;b = 2;new Thread (new Runnable () {@Overridepublic void Run () {/ /TODO auto-generated Method stubmtest = new Test[2];mtest[0] = new test (); mtest[1] = new test (); MTEST[0].TA=3;MTEST[0].TB =4;mtest[1].ta=5;mtest[1].tb=6;}}). Start ();}}

First.java code does not change

public class First {public static void main (String args[]) {Second SE = new second (); System.out.println (se.a+ "&&" +se.b); System.out.println (se.mtest[1].ta+ "&&" +SE.MTEST[1].TB);}}

What should be the result of this? If you say the result is the same, it's wrong to make a mistake.

Accreditations, the results are as follows:

  

Yes, you are not mistaken, I have not passed the wrong picture, is to throw an exception. So the question is, why is it?

If you want to understand this problem, we must first clear the computer's multi-threading mechanism.

In the computer composition principle, we know that the computer multithreading is not a real parallel relationship, strictly speaking, there is only one thread.

For example, the first thought is embroidery (and double-sided embroidery almost), in the embroidered when we only look at one side, but after the embroidery is finished. In fact, the same principle.

First of all, we have only one thread, which is embroidered on both sides with this thread. After a stitch on the front, wear to the reverse and then stitch a stitch, so in turn cycle.

The same is true for multithreaded processing of computers, assuming that there are two threads a,b,a execute for a while, B executes a second. Of course, the computer's threading is not so simple,

If you want to elaborate, the computer assigns a priority to each thread, and the priority determines the order in which the threads are executed and the computer resources allocated to it.

In short, it is strictly said that multithreading is not a parallel relationship, but according to a certain rule of rotation, but the middle of the time unit is very short, the user basically can not detect.

So, the reason why the code throws an exception is that when the main thread executes to the end, our newly opened threads are not finished. So that a null pointer is thrown.

How to solve it? Then the main thread will rest for a while and wait for the new threads to finish executing, call the Sleep function.

So, the modified Java code is as follows:

public class First {public static void main (String args[]) {Second SE = new second (); System.out.println (se.a+ "&&" +se.b); try {thread.sleep (5*1000);} catch (Interruptedexception e) {//TODO Auto-generated catch Blocke.printstacktrace ();} System.out.println (se.mtest[1].ta+ "&&" +SE.MTEST[1].TB);}}

We let the main thread rest 5s. Here is the result of the execution:

The result is this.

Report:

1, the code has a problem do not look for me, is his own long crooked

2. Welcome the criticism

3, reproduced in the source by not to shave a brother Hugh

A probe into multithreading operation in Java

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.