In Java , implementing multithreading requires the use of the Thread class .
Thread class definition
public class Thread extends Object implements Runnable
void |
run() If the thread was constructed using a standalone Runnable run object, the run method of the Runnable object is called; otherwise, the method does nothing and returns. |
void |
start() Causes the thread to start executing; the Java virtual machine calls the thread's run method. |
Implement multithreading method 1:
The custom class inherits the Thread class .
Override run () method in custom class .
Create object .
start thread
There are two scheduling models for threads
time-sharing scheduling model
All threads use the CPU usage rights in turn, and the CPU time slice occupied by each thread is evenly distributed .
Preemptive scheduling model
Threads with higher priorities are given priority to use the CPU. If the priorities of the threads are the same , one will be randomly selected , and the threads with higher priorities will obtain relatively more CPU time slices .
Java uses a preemptive scheduling model .
Methods of the Thread class
Thread priority setting
java.lang.Thread |
|
|
public static final int |
10 |
|
public static final int |
1 |
|
public static final int |
5 |
Thread sleep settings
static void |
sleep(long millis) Sleeps (suspends execution) the currently executing thread for the specified number of milliseconds, subject to the precision and accuracy of system timers and schedulers. |
comity of thread
static void |
yield() Pause the currently executing thread object and execute other threads. |
Thread politeness can make thread allocation more balanced in a certain program .
Daemon thread
void |
setDaemon(boolean on) Mark the thread as a daemon thread or a user thread. |
For programs marked as daemon threads , the daemon threads are also launched after the main thread exits .
thread terminated
public final void stop() Force the thread to stop executing. |
The stop method is officially not recommended , stop will directly stop the thread and will not continue to execute downwards. It has security risks.
kill thread
void |
interrupt the thread. |
Abort the state of the thread and throw an exception .
Implement multi-threading method 2
Custom class implements Runable interface
Override run method in custom class
Create objects of custom classes .
Create an object of the Thread class and pass the object of the custom class as a constructor parameter .
Practice questions , simulate the ticketing program , assuming a total of 100 tickets , there are three windows selling tickets at the same time .
Method 1, the method of inheriting the Thread class
Mode 2, the way to implement the Runable interface .
In order to simulate the real situation , set a delay of 100ms in the program .
As can be seen from the results , there are cases where a ticket is sold twice and negative tickets are sold
Are there any issues that can cause problems ?
Whether the multi-threaded environment
Is there any shared data
Whether multiple statements operate on shared data
This problem can be solved by using synchronize d . The code contained in the synchronized can only be executed by one object at any time.
The synchronize d keyword can also modify methods
Lock object for synchronized code blocks
any object
Synchronized method format and lock object
Add the synchronization keyword to the method .
What is the object of the synchronized method ?
this, that is to say, if the same code needs to be locked outside the method, the synchronization object this is required.
Static method and lock object problem
The lock object of the static method is the bytecode file object of the class . ( eg SellTicket.class)
Methods for creating thread-safe collection classes