您的位置:首页 > 编程语言 > Java开发

JAVA多线程编程——1.9线程让步-1.11守护线程

2017-12-04 19:14 267 查看

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对象也不再打印了,也就是停止了
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  java 多线程 线程 编程