What is a program?
A set of instructions installed on disk, which is a static concept.
What is a process?
It is a running program, is a dynamic concept, each process has a separate resource space.
What is a thread?
A thread, also known as a lightweight process, is the smallest unit of program execution flow and a single sequential control flow in a program. A thread is an entity of a process, and is the basic unit that is dispatched and dispatched independently by the system.
What is multithreading?
Multi-threading refers to the ability to run multiple different threads at the same time in a single program to perform different tasks.
Features of multithreading
① a process can contain one or more threads.
② A program that implements multiple code runs concurrently requires multiple threads to be generated.
The ③ thread itself does not own system resources and shares the resources owned by the process with other threads belonging to one process.
④ can execute concurrently between multiple threads in the same process. The CPU randomly takes the time to get our program to do it and do another thing.
The purpose of multithreading
is to "make the most of CPU resources," and when a thread's processing does not need to consume CPU and only deal with resources such as I/O, let other threads that consume CPU resources have the opportunity to gain CPU resources. Fundamentally, this is the ultimate goal of multithreaded programming.
The Java operating system relies on threads in many ways, and all class library designs take into account multithreading. Java is a purely object-oriented language, and Java's threading model is also object-oriented.
To Create a thread :
Create a thread by inheriting the thread class
① ordinary Java classes, such as inheriting from the thread class, become a thread class and can be started by the class's Start method to execute thread code.
Subclasses of the ②thread class can be instantiated directly, but the subclass must override the Run method to actually run the thread's code.
Creating a thread by implementing the Runnable interface
① classes that implement runnable interfaces must use the thread class to create threads. Creating a thread from the Runnable interface is two steps:
A) Create an instance of the class that implements the Runnable interface.
b) Create a thread class object, passing the Runnable object instantiated in the first step as a parameter to the constructor of the thread class.
② starts the thread through the start method of the thread class.
Code:
Inheritance Thread Creating Threads
1 classHellothreadextendsThread {2 PublicHellothread (String name) {3 Super(name);4 }5 @Override6 Public voidrun () {7 for(inti = 0; I < 5; i++) {8 //gets the name of the thread9System.out.println ( This. GetName () + ":" +i);Ten } One } A}
Main method:
1Hellothread H1 =NewHellothread ("a");//To set the thread name in the constructor method2 //h1.setname ("Thread 1");//set thread name (default construct)3H1.start ();//Start Thread4Hellothread h2=NewHellothread ("B");5 //h2.setname ("Thread 2");6H2.start ();
Executes the program several times, the execution result is different, the instruction thread once opens, executes randomly.
Creating a thread using the Runnable interface
1 classHellorunnableImplementsRunnable {2 3 @Override4 Public voidrun () {5 for(inti = 0; I < 5; i++) {6 //gets the name of the thread7System.out.println (Thread.CurrentThread (). GetName () + ":" +i);8 }9 }Ten One}
Main method:
1 New hellorunnable (); 2 New Thread (hellorunnable, "A"); 3 T1.start (); 4 New Thread (hellorunnable, "B"); 5 T2.start ();
There are 2 benefits of creating threads using the Runnable interface:
1, to avoid the limitations of single inheritance, a class can implement multiple interfaces, but can only inherit a class
2. Suitable for resource sharing
Example: Sell 5 tickets in 2 windows:
When you create a thread using thread:
1 classTickthreadextendsThread {2 Private intTicket = 5;3 4 PublicTickthread (String name) {5 Super(name);6 }7 8 @Override9 Public voidrun () {Ten while(true) { OneSystem.out.println ( This. GetName () + ":" + (ticket--)); A if(ticket<1){ - Break; - } the } - } -}
Main method:
1 Tickthread s1=New tickthread ("one Window"); 2 S1.start (); 3 Tickthread s2=New tickthread ("Window No. second"); 4 S2.start ();
After execution, it was found that 5 tickets were sold in each of the 2 windows.
When creating a thread using the Runnable interface:
1 classTickrunnableImplementsrunnable{2 Private intTicket = 5;3 4 @Override5 Public voidrun () {6 while(true) {7System.out.println (Thread.CurrentThread (). GetName () + ":" + (ticket--));8 if(ticket<1){9 Break;Ten } One } A } - -}
Main method:
1 Tickrunnable runnable=New tickrunnable (); 2 Thread t1=new thread (runnable, "one Window"); 3 T1.start (); 4 Thread t2=new thread (runnable, "window number second"); 5 T2.start ();
After execution you can see that 5 tickets are sold together by 2 windows because only one object was created
Multithreading--Threading model