Process: The application being executed. The amount of space a memory allocates when an application runs.
Thread: A control unit executed by a program in a process, an execution path. Responsible for the execution order of the program. The meaning of multithreading exists: at least two threads are running in the program, one is the main thread of the primary function and the other is the garbage collection thread. Thread Creation Method One: inherits the thread class. To overwrite its run method, call the threading Start method. function: 1. Start thread 2. Run the running method. The goal is to store the custom code in the Run method, so that the thread runs the CPU one program at a time, but switches between different threads quickly, showing the randomness of multithreading
classMlpcextendsthread //First step, define class inheritance Thread class {Private Static intTicket = 100; Public voidRun () ///Step two, overwrite the Run method and write the code that needs to be run in the field { while(true) { if(ticket>0) {System.out.println (Thread.CurrentThread ()+ "" +ticket--); } } }} classMlpcdemo { Public Static voidMain (string[] args) {MLPC M1=NewMLPC (); ///Third step, create a thread object by creating a subclass object of the thread class. mlpc m2=NewMLPC (); MLPC M3=NewMLPC (); MLPC M4=NewMLPC (); M1.start (); //Fourth step, call the Start method, open the thread, execute the Run method M1.start (); M1.start (); M1.start (); }}
The Run method and the Start method Run method is just the object invocation method, and the Start method is not running thread and the run method in the thread is started
Dark Horse Programmer-Learning Diary (multithreading)