Why does java-17.1 require concurrency?

Source: Internet
Author: User

Why does java-17.1 require concurrency?

In this chapter, we will discuss why concurrency is required?

Answer: speed up and promote code design improvements

1. Fast

First, compare the following two examples:

Package com. ray. ch17; import java. util. arrayList; public class Test {public static void main (String [] args) throws InterruptedException {long startTime = System. currentTimeMillis (); System. out. println ("-- program start --"); System. out. println ("working... "); ArrayList
 
  
List = new ArrayList
  
   
(); For (int I = 0; I <10; I ++) {list. add (I); Thread. sleep (100); // time required for some operations} ArrayList
   
    
List2 = new ArrayList
    
     
(); For (int I = 0; I <10; I ++) {list2.add (I); Thread. sleep (100); // time required for some operations} System. out. println ("-- program end --"); long endTime = System. currentTimeMillis (); System. out. println ("" + (endTime-startTime ));}}
    
   
  
 


Output:

-- Program start --
Working...
-- Program end --
2015

Package com. ray. ch17; import java. util. arrayList; import java. util. concurrent. countDownLatch; public class Test2 {public static void main (String [] args) throws InterruptedException {long startTime = System. currentTimeMillis (); System. out. println ("-- program start --"); System. out. println ("working... "); CountDownLatch countDownLatch = new CountDownLatch (2); ThreadOne threadOne = new ThreadOne (countDownLatch); ThreadTwo threadTwo = new ThreadTwo (countDownLatch); Thread thread1 = new Thread (threadOne ); thread thread2 = new Thread (threadTwo); thread1.start (); thread2.start (); countDownLatch. await (); System. out. println ("-- program end --"); long endTime = System. currentTimeMillis (); System. out. println ("" + (endTime-startTime) ;}} class ThreadOne implements Runnable {private CountDownLatch countDownLatch; public ThreadOne (CountDownLatch countDownLatch) {this. countDownLatch = countDownLatch;} @ Overridepublic void run () {ArrayList
     
      
List = new ArrayList
      
       
(); For (int I = 0; I <10; I ++) {list. add (I); try {Thread. sleep (100); // time required for some operations} catch (InterruptedException e) {e. printStackTrace () ;}} countDownLatch. countDown () ;}} class ThreadTwo implements Runnable {private CountDownLatch countDownLatch; public ThreadTwo (CountDownLatch countDownLatch) {this. countDownLatch = countDownLatch;} @ Overridepublic void run () {ArrayList
       
        
List = new ArrayList
        
         
(); For (int I = 0; I <10; I ++) {list. add (I); try {Thread. sleep (100); // time required for some operations} catch (InterruptedException e) {e. printStackTrace () ;}} countDownLatch. countDown ();}}
        
       
      
     


Output:

-- Program start --
Working...
-- Program end --
1016

From the output of the above two examples, we can see that everyone is also adding data to the two lists, there is operation time in the middle, but the second uses concurrency, that is, the example of Multithreading is obviously faster than the one above.

Some of our operations inevitably need to wait. If we follow the first example, blocking will occur, and operations are completed one by one in order, not insurmountable, but the second method uses multithreading to make full use of the current multi-cup to process tasks. It is no longer a single-thread linear operation.

The above example is just an example of small data size. When it reaches the level of processing millions or even hundreds of millions, the performance of multithreading will be more displayed.

 

 

2. Promote code design improvements

Example:

 

Package com. ray. ch16; public class Test {public static void main (String [] args) {// operation 1 // operation 2 // operation 3 }}

In normal programming, we often unconsciously place a large number of operations in the same method, like the code above.

 

However, after the introduction of concurrency, since only one operation requires concurrency and other operations are not required, now programmers should break down and process this method to form various methods, then the decoupling between the methods is performed.

 

Package com. ray. ch16; public class Test {public void methodA () {// Action 1} public synchronized void methodB () {// Action 2} public void methodC () {// Action 3} public void method () {methodA (); methodB (); methodC ();} public static void main (String [] args) {new Test (). method ();}}

The above code is the improved Code. In addition, when the above method is broken down, in fact, every small method may, even if one operation of other methods, decouple other methods, finally, the overall code improvement is achieved.

 

 

Conclusion: This chapter compares two examples to find out why concurrency is required.

 

This chapter is here. Thank you.

 

Related Article

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.