First, why need concurrent programming
In the case of single-threaded programming, if a program encounters a blocking situation, such as an event that waits for I/O to occur, the program can be executed. This will affect the operation of the following programs.
Concurrency is the process in which multiple tasks are processed and each task is switched back and forth from the operating system.
It feels like many tasks are executed at the same time.
Second, the basic threading mechanism
1. Define Tasks
Define a class, implement the Runnable () interface, define the run () method in the runnable () interface, and we can write the event to be executed in the run () method.
The run of the task in run () needs to be placed in the thread constructor.
After you run the thread through the Start method, the tasks in thread are run.
Class Task1 implements runnable{public void Run () {for (int. x=0;x<=10;x++) {for (int y=0;y <=99999999;y++) {}system.out.println (Thread.CurrentThread (). GetName () + ".... x=" +x);}} Class Task2 implements runnable{public void Run () {for (int. z=0;z<=10;z++) {for (int y=0;y <=99999999;y++) {}system.out.println (Thread.CurrentThread (). GetName () + ".... z=" +z);}} public class Hello {public static void main (string[] args) {Task1 t1= new Task1 (); Task2 t2 = new Task2 (); Thread nt1 = new Thread (t1); Thread nt2 = new Thread (t2); Nt1.start (); Nt2.start ();}}
Java concurrent Programming--basic threading mechanism 1