Java multi-thread sequential execution

Source: Internet
Author: User

The hateful Java multithreading has never understood the role of synchronization!

I always thought that during synchronization, as long as the objects are locked, they can be executed in sequence:


[Java]
Public class Test {
Final static byte [] B = new byte [0];
 
Public static void main (String [] args ){
Test t = new Test ();
T. thread. start ();
Test t2 = new Test ();
T2.thread. start ();
}
 
Thread thread = new Thread (new Runnable (){
@ Override
Public void run (){
Test ();
}
});
 
Public void test (){
Synchronized (this ){
For (int n = 0; n <100; n ++ ){
System. out. println (thread. getName () + ":" + n );
Try {
Thread. sleep (1000 );
} Catch (InterruptedException e ){
E. printStackTrace ();
}
}
}
}
}

Public class Test {
Final static byte [] B = new byte [0];

Public static void main (String [] args ){
Test t = new Test ();
T. thread. start ();
Test t2 = new Test ();
T2.thread. start ();
}

Thread thread = new Thread (new Runnable (){
@ Override
Public void run (){
Test ();
}
});

Public void test (){
Synchronized (this ){
For (int n = 0; n <100; n ++ ){
System. out. println (thread. getName () + ":" + n );
Try {
Thread. sleep (1000 );
} Catch (InterruptedException e ){
E. printStackTrace ();
}
}
}
}
}
But this is wrong! The two threads are still executed in turn!

 


After reading a lot of information, we can see that the above lock is incorrect. The this object locked by the two threads is not the same, which leads to cross execution. It should be changed:


[Java]
Public class Test {
Final static byte [] B = new byte [0];
 
Public static void main (String [] args ){
Test t = new Test ();
T. thread. start ();
T. test ();
}
 
Thread thread = new Thread (new Runnable (){
@ Override
Public void run (){
Test ();
}
});
 
Public void test (){
Synchronized (this ){
For (int n = 0; n <100; n ++ ){
System. out. println (thread. getName () + ":" + n );
Try {
Thread. sleep (1000 );
} Catch (InterruptedException e ){
E. printStackTrace ();
}
}
}
 
}
}

Public class Test {
Final static byte [] B = new byte [0];

Public static void main (String [] args ){
Test t = new Test ();
T. thread. start ();
T. test ();
}

Thread thread = new Thread (new Runnable (){
@ Override
Public void run (){
Test ();
}
});

Public void test (){
Synchronized (this ){
For (int n = 0; n <100; n ++ ){
System. out. println (thread. getName () + ":" + n );
Try {
Thread. sleep (1000 );
} Catch (InterruptedException e ){
E. printStackTrace ();
}
}
}

}
}
This is indeed a sequential execution, because this object locked by the function is the same. If thread synchronization is removed, it will be executed in crossover.

But what I still don't understand is that the error in the first example is that the locked object is not the same, but it is still incorrect if I change this to a Global static constant! Theoretically, there is only one object in the memory of the Global static constant throughout the runtime! I don't understand why! Hope you can answer this question.

 


In fact, most of the synchronization tasks are used for thread execution in sequence. For example, during Android development, we usually want the foreground to display a progress box and the background thread to execute the download action, after the download is complete, the foreground thread executes the remaining operations, as shown in the interface.

In this case, the join () of the thread can be used ()!


You can check the api of the oin method. It means that other threads are executed only after the execution of the current thread is completed. That is to say, if a class has such a code segment:

Thread1.start ();

Thread2.start ();

Thread1.join ();

Thread2.join ();

Do something 1;

Do something 2;

Then this code will wait until the two threads are executed and then execute do something 1 and do something 2. Note: All threads must be started before join. If you start a join operation, what is the result? Yes, it will wait until thread1 is executed and then execute thread2, and then execute the subsequent code.


 

Author: h3c4lenovo

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.