Multi thread 2[thread control]
1.Thread scheduling and setting thread priority
(1).Two models of thread scheduling
A:In the time-sharing scheduling model, all threads take turns to use CPU usage and allocate the time slice of each thread occupying CPU.
B:The preemptive scheduling model gives priority to the use of CPU by high priority threads. If the priority is the same, then at this time a randomly selected CPU is obtained by high priority threads.
The time slice is relatively small.
javaThe preemptive scheduling model is used.
(2).thread priority
A:Get priority of thread
public final int getPriority():Return priority of thread
//The default is 5
B:Setting thread priority
public final int setPriority(int i):Setting thread priority
//The priority range of threads is: 1-10
Exception: java. lang. IllegalArgumentException: Illegal parameter exception, indicating that an incorrect parameter value was passed to the method
C:Matters needing attention
Threads with high priority only indicate a high probability of getting time slices of the CPU. Because of the randomness, the effect can only be seen in multiple tests.
(3).code implementation
//Thread class
public class MyThread extends Thread{
@Override
public void run() {
//super.run();
for (int i = 0; i < 1000; i++) {
System.out.println(getName()+”—“+i);
}
}
//Test class
public class ThreadTest {
public static void main(String[] args) {
MyThread mt =new MyThread();
MyThread mt1 =new MyThread();
mt.setName(“* “big pig”);
mt1.setName(“The piglet “);
//Set thread priority
mt.setPriority(10);
mt1.setPriority(1);
//Get default priority
//System.out.println(mt.getPriority());
//System.out.println(mt1.getPriority());
mt.start();
mt1.start();
}
}
2.Thread controlled sleep thread
(1).Method
public static void sleep(long millis);//Thread hibernation time allows the current thread to pause for a specified time in milliseconds.
(2).code implementation
//Thread class
public class MyThread extends Thread{
@Override
public void run() {
//super.run();
for (int i = 0; i < 1000; i++) {
System.out.println(getName()+”—“+i+”,The time is: “+new Date ());
//sleep
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
//Test class
public class ThreadTest {
public static void main(String[] args) {
MyThread mt =new MyThread();
MyThread mt1 =new MyThread();
MyThread mt2 =new MyThread();
mt.setName(“* “big pig”);
mt1.setName(“The piglet “);
mt2.setName(“Fat pig *)
mt.start();
mt1.start();
mt2.start();
}
}
3.Thread of thread control
(1).When do you use join threads?
When you have a priority, you can consider joining threads. That is, one of the threads has run out, other threads can rob the CPU to use, this situation can be used.
(2).Thread join method
public final void join()
throws InterruptedException:Wait for the thread to terminate.
(3).code implementation
//Thread class
public class MyThread extends Thread{
@Override
public void run() {
//super.run();
for (int i = 0; i < 1000; i++) {
System.out.println(getName()+”—“+i);
}
}
}
//Test class
public class ThreadTest {
public static void main(String[] args) {
MyThread mt =new MyThread();
MyThread mt1 =new MyThread();
MyThread mt2 =new MyThread();
mt.setName(“* “big pig”);
mt1.setName(“The piglet “);
mt2.setName(“Fat pig *)
mt.start();
try {
mt.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
mt1.start();
mt2.start();
}
}
//Result
Pigs are executed first, then * * * and fat pigs grab CPU.
4.Thread control threads
(1).Courtesy thread method
public static void yield():Suspend the thread object being executed and execute other threads.
(2).Conditions for courtesy threads
Let threads be in harmony to a certain extent, that is, thread A executes, thread B executes, then thread A, and then thread B…, which only works to a certain extent (one person at a time).
(3).code implementation
//Thread class
public class MyThread extends Thread{
@Override
public void run() {
//super.run();
for (int i = 0; i < 1000; i++) {
System.out.println(getName()+”—“+i);
//Setting courtesy threads
Thread.yield();
}
}
}
//Test class
public class ThreadTest {
public static void main(String[] args) {
MyThread mt =new MyThread();
MyThread mt1 =new MyThread();
MyThread mt2 =new MyThread();
mt.setName(“* “big pig”);
mt1.setName(“The piglet “);
mt2.setName(“Fat pig *)
mt.start();
mt1.start();
mt2.start();
}
}
5.Backstage thread
(1).Background thread method
public static void setDaemon(boolean on):Mark the thread as either a daemon thread or a user group thread, and the Java virtual machine exits when the running thread is a daemon thread
Note: this method must be invoked before starting the thread.
(2).code implementation
//Thread class
public class MyThread extends Thread{
@Override
public void run() {
//super.run();
for (int i = 0; i < 1000; i++) {
System.out.println(getName()+”—“+i);
}
}
}
//Test class
public class ThreadTest {
public static void main(String[] args) {
//Other threads execute content
MyThread mt =new MyThread();
MyThread mt1 =new MyThread();
mt.setName(“Guan Yu “);
mt1.setName(“”Zhang Fei”;
//Marked as daemon thread (Guardian main thread)
mt.setDaemon(true);
mt1.setDaemon(true);
mt.start();
mt1.start();
//Main thread execution content
Thread.currentThread().setName(“Liu Bei “);
for(int i=0;i<5;i++){
System.out.println(Thread.currentThread().getName()+”—-“+i);
}
}
}
//Guarding the main thread, when the for () loop is executed to 4, the other two threads will stop.
6.Interrupt thread for thread control
(1).Interrupt thread
public final void stop():Ending a thread (obsolete, but still usable) is insecure, exits directly, and does not execute subsequent code.
public void interrupt();End a thread and continue to execute subsequent code by throwing an exception.
(2).code implementation
//Thread class
public class MyThread extends Thread{
@Override
public void run() {
//super.run();
System.out.println(“Start “+new Date ());
//Rest 10 seconds
try {
Thread.sleep(10000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
//e.printStackTrace();
System.out.println(“Rest for 10 seconds.
}
System.out.println(“End “+new Date ());
}
}
//Test class
public class ThreadTest {
public static void main(String[] args) {
//Other threads execute content
MyThread mt =new MyThread();
mt.setName(“Three fat “);
mt.start();
//If you don’t execute code for more than three seconds, you end the thread.
try {
Thread.sleep(3000);
//mt.stop();
mt.interrupt();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
//Using stop () method, output results:
Start Sat Sep 01 00:04:27 CST 2018
//Use interrupt () method to output results.
Start Sat Sep 01 00:04:27 CST 2018
Rest for 10 seconds.
End Sat Sep 01 00:04:30 CST 2018
7.Life cycle of threads