A Java question about threads

Source: Internet
Author: User

The following is a question about Java threads.

The following is a Java class code:

1 public class Test {
2
3 int a = 0;
4 int B = 0;
5 int x = 0;
6 int y = 0;
7
8 public static void main (String [] args) throws InterruptedException {
9 Test test = new Test ();
10 test. run ();
11}
12
13 private void run () throws InterruptedException {
14 Thread th1 = new Thread (){
15 public void run (){
16 a = 1;
17 y = B;
18}
19 };
20 th1.start ();
21
22 Thread Th1 = new Thread (){
23 public void run (){
24 B = 1;
25 x =;
26}
27 };
28 th2.start ();
29
30 th1.join ();
31 th2.join ();
32
33 System. out. println ("x =" + x + ", y =" + y );
34}
35}

The output result after running is 【]

A. x = 0, y = 0 B. x = 0, y = 1 C. x = 1, y = 0 D. x = 1, y = 1

 

For this question, there may be multiple answers:

A) if you have no knowledge about the thread, the answer should be C;

B) If you have knowledge about the thread, the answer should be a combination of any two or three choices in B, C, and D;

C. Anyone who knows about both the thread and JVM operating mechanism has only one ABCD answer.

 

Many children's shoes may have doubts. Why is ???

 

I wrote a piece of code to run for consecutive times. The number of times ABCD appears is as follows:

A: 13, B: 240662, C: 9759320, D: 5

 

Next I will explain why the above possibilities occur (one by one according to the difficulty ):

C. This is the simplest. I will not explain it here. The result of JVM execution in the order of code.

 

B. Explain the start method first. When the program calls the start method, the JVM creates a new thread and calls the init method inside the thread. The init method will first determine the operations performed by the thread in the specified period by SecurityManager. If no operations are performed, the current thread will obtain the corresponding ThreadGroup Thread Tree (where the main thread already exists ), then, check whether the init method is a daemon thread to obtain the thread priority, automatically generated nextThreadID, and stackSize. After this is done, the current thread is in the ready state. There will be two threads starting to execute: one is the main thread and the other is the thread created by the main thread calling the start method. The newly created thread will be added to the ThreadGroup. At this time, the CPU will flow through the time slice to execute the current thread based on the priority of the current thread. The time slice is very short, so it feels like it will be executed immediately.

Therefore, in the above Code, although the thread th1 declares and calls the start method before the 2nd method, due to the thread priority, it is possible that the 2nd thread runs before the th1 thread, in this way, the result after execution is B.

 

D. after thread 1 runs 16th rows, thread 2 may be run next due to CPU scheduling resource competition and other reasons. After running 24th rows, thread 2 code is run again, in this way, the result of D is displayed.

 

A. Many people are puzzled about the result of A. The reason is actually very simple. When JVM executes compiled java code, if the execution sequence of a code block is changed without affecting the final result, JVM may not necessarily execute the code in the order of code, that is to say, the execution sequence of the Code in lines 16th and 17th may be reversed. Then you can get the answer A by referring to the explanation of D.

 

This is the end. If any explanation is incorrect, please make a brick and feel free to contact us.

 

 

Source: Java concurrent programming practices.

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.