A process can have multiple threads, such as a video, you can hear the sound at the same time, look at the image to see the barrage, and so on.
- Speaking of the process, we have to say under the program . Program is an ordered set of instructions and data, which does not have any operational meaning, is a static concept.
- The process is executed once during the execution of Xu Chun, it is a dynamic concept. It is a unit of allocation of system resources.
- Generally can be included in a process in several threads , of course, there is a process in at least one thread, otherwise there is no meaning of existence. A thread is a unit of CPU scheduling and execution.
Note: Many multi-threading is simulated, and true multi-threaded means there are multiple CPU, namely multi-core, such as servers. If it is multi-threaded simulation out that in the case of a CPU, and at the same time point, a CPU can execute code, since switching quickly, so there will be a simultaneous execution of illusion.
Key concept
- Thread is an independent execution path;
- The program runs, im not create your own thread, the background will have multiple threads, such as the main thread, GC thread;
- main () is called the main thread, the inlet system, for executing the entire program;
- In one process, if opened up multiple threads, the thread running schedule scheduled by the scheduler, the scheduler is closely related to the operating system, the order is not considered intervention;
- When a resource of the same operation, there will be problems of resource grab, if required concurrency control;
- Thread would bring additional costs, such as CPU time scheduling, concurrency control overhead;
- Each thread in his interaction working memory, memory controller improperly can cause inconsistent data;
Three ways to create threads ways:
Code implementation, I do not recommend the use of "inheritance Thread class" to avoid the limitations of single inheritance OOP recommended "implement Runnable" because it avoids the limitations of single inheritance, flexible, easy to use is the same object multiple threads. as follows:
A code for implementation:
1 Package Penalty for com.huolongluo.coindemo.morethread.sub1; 2 3 / ** 4 * the Created by naked flames 2019/11/9 ON. 5 * desc: a way to create threads: Thread class inheritance, override the run method calls start open thread. 6 * 7 * Summary: open thread does not necessarily execute immediately executed by the CPU scheduling . 8 * Version: 1.0 . 9 * / 10 public class TestThread1 the extends {the Thread . 11 @Override 12 is public void RUN () { 13 is // RUN method of thread member 14 for ( int I = 0; I <20 is; I ++ ) { 15 System.out.println ( "I see the code ---" + I); 16 } . 17 } 18 is . 19 public static void main (String [] args) { 20 is // main thread, the main thread 21 is 22 is // Create a thread object 23 TestThread1 TestThread1 = new new TestThread1 (); 24- // call the start () method to open a thread 25 // attention to this place, if you call the run method, although it will execute the run inside the code, but it is called, we will first execute End run inside the code, and finally print "today is a day of struggle", execute multiple threads at the same time, you should use the start method, so that they will be performed alternately. 26 is testThread1.start (); 27 28 for ( int0 = I; I <20 is; I ++ ) { 29 System.out.println ( "today is the day struggle -" + I); 30 } 31 is } 32 }
Code implementation way:
. 1 Package com.huolongluo.coindemo.morethread.sub1; 2 . 3 / ** . 4 * the Created by naked flames 2019/11/9 ON. . 5 * desc: create a thread way: implement Runnable, override run method, a thread of execution need to dispose of runnable interface implementation class, call the start method. . 6 * Version: 1.0 . 7 * / . 8 public class TestThread2 the implements the Runnable { . 9 @Override 10 public void RUN () { . 11 // RUN method thread member 12 is for ( int I = 0; I <20 is; I ++ ) { 13 is the System. out.println ( "I look at the code ---" +I); 14 } 15 } 16 . 17 public static void main (String [] args) { 18 is // create an object class that implements interface runnable . 19 TestThread2 TestThread2 = new new TestThread2 (); 20 is // create a thread object, thread object we open thread, the agent 21 is // the thread thread new new = the thread (TestThread2); 22 is // Thread.start (); 23 is new new the thread (TestThread2) .start (); 24 for ( int I = 0; I <20 is; ++ I ) { 25 System.out.println ( "Today is a day of struggle -" + i); 26 } 27 } 28 }
When you need multiple threads operating the same object that it needs to find ways to deal with "concurrency issues", the code examples are as follows:
. 1 Package com.huolongluo.coindemo.morethread.sub1; 2 . 3 / ** . 4 * the Created by naked flames 2019/11/9 ON. . 5 * desc: multiple threads are operating the same object 6 * buy tickets Examples 7 * < P> . 8 * in case a plurality of threads operating the same resource object, thread-safe, data disorder. . 9 * Version: 1.0 10 * / . 11 public class TestThread4 the implements the Runnable { 12 is 13 is // votes 14 Private int ticketNums = 10 ; 15 16 @Override . 17 public voidRUN () { 18 is the while ( to true ) { . 19 IF (ticketNums <= 0 ) { 20 is BREAK ; 21 is } 22 is // analog delay 23 is the try { 24 the Thread.sleep (200 is ); 25 } the catch (InterruptedException E) { 26 is e.printStackTrace (); 27 } 28 System.out.println (Thread.currentThread () getName () + "get the first:". + ticketNums-- + "tickets" ); 29 } 30 } 31 32 public static void main(String[] args) { 33 34 TestThread4 testThread4 = new TestThread4(); 35 36 new Thread(testThread4, "小明").start(); 37 new Thread(testThread4, "老师").start(); 38 new Thread(testThread4, "黄牛党").start(); 39 } 40 }
Results of the:
Seen from the execution result, abnormal ticket order, sixth two people to get tickets simultaneously, but also the emergence of 0 tickets. This is the need to pay attention to handling multi-threaded concurrency issues.