[Concurrency] Java concurrent programming entry-1

Source: Internet
Author: User

[Why use concurrency ?]
1. ImproveProgramRunning Speed
When running on a multi-core or multi-processor system, concurrent programs can take full advantage of multiple execution units to speed up; however, when running concurrent programs on a single processor, there are two scenarios: when there is a blocking in the Program (for example, I/O blocking), concurrency can increase the speed. If it is a CPU-consuming operation without blocking, running a concurrent program on a single processor will not only increase the speed, but will increase the running time of the program, because switching between threads increases the overhead of the system. When running on a multi-processor, although there are thread switching overhead, the performance improvement brought by concurrency is much higher than this overhead.
2. Simplified Design
In reality, some operations are performed concurrently. Therefore, using concurrency for program design can simulate actual operations and simplify program design.

The most common example of concurrency in Java is GUI event-driven programming. Using concurrency can significantly improve the response line on the user interface. If no concurrency is used, all event handlers in the program have to cyclically detect user input. Otherwise, before the current operation (which may be a very time-consuming operation) is completed, other user input will not be responded. When concurrency is adopted and a single thread is used to execute a response to user input, each event can always get a certain program response. In fact, this still depends on the operating system's concurrency model. if we adopt a Time chip mechanism similar to Windows, we can ensure that each thread can be executed within a certain period of time, however, if the Solaris FIFO concurrency model is used, the tasks with the same priority will be executed only after they are executed or blocked.

[How to Use threads in Java]
Two methods:
1. define a class to implement the runnable interface, that is, to implement the run () method to define the operation to be executed in this thread, and then use this class of object as the parameter to create a thread object T and call T. the START () method can be used to execute the operations defined in the run () method in this thread.
2. inherit the Thread class directly and override the run () method.

1 Public   Class Simplethreadtest {
2 /**
3 * @ Param ARGs
4 */
5 Public   Static   Void Main (string [] ARGs ){
6 // Todo auto-generated method stub
7   Thread t = New Thread ( New Runnable (){
8 Public   Void Run (){
9 Int I = 0 ;
10 While ( True ){
11 System. Out. println ( " In the thread1: " + I );
12 I ++ ;
13 If (I = 5 ){
14 Break ;
15 }
16 }
17 }
18 });
19 T. Start ();
20 For ( Int I = 0 ; I < 5 ; I ++ ){
21 System. Out. println ( " In the main thread: " + I );
22 }
23 }
24 }

Running result:

1 In the main thread: 0
2 In the main thread: 1
3 In the main thread: 2
4 In the main thread: 3
5 In the main thread: 4
6 In the thread1: 0
7 In the thread1: 1
8 In the thread1: 2
9 In the thread1: 3
10 In the thread1: 4

ExampleCodeThe anonymous class is used. In actual use, a new class can be defined to implement the runnable interface or directly inherit the Thread class and override the run () method.

To be continued...

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.