1.9線程讓步(yield方法)
yield()方法的作用是當前線程放棄CPU資源,然後重新選擇運行哪個線程。
public class Thread_191 {
static class MyThread extends Thread{
public void run(){
long beginTime = System.currentTimeMillis();
int count =0;
for(int i=0;i<5000000;i++){
//Thread.yield();
count+=i+1;
}
long endTime = System.currentTimeMillis();
System.out.println("用了: "+(endTime-beginTime)+"毫秒! ");
}
}
public static void main(String[] args) {
MyThread myThread= new MyThread();
myThread.start();
}
}
1.10線程優先級
1.線程優先級是可以繼承的。
/**
* Created by Senliang-Ying on 2017/11/28.
*/
public class Thread_1101 {
static class MyThread1 extends Thread{
public void run(){
System.out.println("MyThread1 run priority =" + this.getPriority());
MyThread2 myThread2 = new MyThread2();
myThread2.start();
}
}
static class MyThread2 extends Thread{
public void run(){
System.out.println("MyThread2 run priority =" + this.getPriority());
}
}
public static void main(String[] args) {
System.out.println("main thread begin priority = "+ Thread.currentThread().getPriority());
Thread.currentThread().setPriority(6);
System.out.println("main end priority= "+Thread.currentThread().getPriority());
MyThread1 myThread1 = new MyThread1();
myThread1.start();
}
}
2.設置優先級(setPriority)
import java.util.Random;
public class Thread_1102 {
static class MyThread1 extends Thread {
public void run() {
// System.out.println("MyThread1 run priority =" + this.getPriority());
long beginTime = System.currentTimeMillis();
long addResult = 0;
for (int j = 0; j < 10; j++) {
for (int i = 0; i < 50000; i++) {
Random random = new Random();
random.nextInt();
addResult = addResult + i;
}
}
long endTime = System.currentTimeMillis();
System.out.println("⭐️ ⭐️ ⭐️ ⭐️ thread 1 use time= " + (endTime - beginTime));
}
}
static class MyThread2 extends Thread {
public void run() {
long beginTime = System.currentTimeMillis();
long addResult = 0;
for (int j = 0; j < 10; j++) {
for (int i = 0; i < 50000; i++) {
Random random = new Random();
random.nextInt();
addResult = addResult + i;
}
}
long endTime = System.currentTimeMillis();
System.out.println("⭐️ ⭐️ ⭐️ ⭐️ thread 2 use time= " + (endTime - beginTime));
}
}
public static void main(String[] args) {
for (int i = 0; i < 5; i++) {
MyThread1 thread1= new MyThread1();
thread1.setPriority(10);
thread1.start();
MyThread2 thread2 = new MyThread2();
thread2.setPriority(4);
thread2.start();
}
}
}
多次運行後,可以發現優先級高的線程大多數情況下優先執行。
1.11守護線程
JAVA中有兩種線程,一種是用戶線程,一種是守護線程(Daemon),守護線程就是一種陪伴用戶線程的線程,只有當一個用戶線程都沒有的情況下,守護線程纔會停止,垃圾回收機制就是一個典型的守護線程。可以用setDeamon()方法對線程對象進行設置。代碼如下
/**
* Created by Senliang-Ying on 2017/12/4.
* 守護線程
*/
public class Thread_1103 {
static class MyThread extends Thread {
private int i = 0;
@Override
public void run() {
try {
while (true) {
i++;
System.out.println("i= " + i);
Thread.sleep(1000);
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
public static void main(String[] args) {
try{
MyThread thread = new MyThread();
thread.setDaemon(true);//設置爲守護線程
thread.start();
Thread.sleep(5000);
System.out.println("我離開thread對象也不再打印了,也就是停止了");
}catch(InterruptedException e){
e.printStackTrace();
}
}
}
輸出如下:
i= 1
i= 2
i= 3
i= 4
i= 5
我離開thread對象也不再打印了,也就是停止了