改变线程的优先级
Thread.sleep(5000);// 在哪个线程中调用类线程的sleep(),哪个线程就会睡觉
线程的优先级只要知道就可以了,用默认的5级就足够。
千万不要改变线程的优先级,否则若是虚拟机中垃圾回收机制被饿死了不能执行,导致虚拟机内存耗尽崩溃。
任意一个项目
main方法:
public class Main {
/**
* @param args
*/
public static void main(String[] args) {
Thread1 t1 = new Thread1("小黑线程-最低优先级");
Thread2 t2 = new Thread2("小红线程-最高优先级");
// 改变线程的优先级
// 线程的优先级只要知道就可以了,用默认的5级就足够,千万不要改变线程的优先级
t1.setPriority(Thread.MIN_PRIORITY);
t2.setPriority(10);
t1.start();
t2.start();
}
}
线程1:public class Thread1 extends Thread {
public Thread1(String name) {
super(name);
}
// 我们把线程要执行的操作写在run方法中
// run()方法又被称为线程体
// 取得优先级并显示
// 优先级越高被线程调度器选中执行的机会越高
public void run() {
for (int i = 1; i <= 100000; i++) {
System.out.println(this.getName() + "在计数:i=" + i + ",线程的优先级是" + this.getPriority());
}
}
}
线程2:
public class Thread2 extends Thread {
public Thread2(String name) {
super(name);
}
// 我们把线程要执行的操作写在run方法中
// run()方法又被称为线程体
public void run() {
for (int i = 1; i <= 100000; i++) {
System.out.println(this.getName() + "在计数:i=" + i + ",线程的优先级是" + this.getPriority());
}
}
}
上面代码可以执行测试改变线程的优先级。
Thread.yield();//注意:线程的sleep()/yield()方法都是静态方法.静态方法调用的时候和对象无关
暂停当前正在执行的线程.该线程重新回到了就绪队列,又有可能被线程调度器重新选中。
即使这样的话,相对于其他的线程来说,也是让其他的线程更多的获得了运行机会。
任意一个项目
main方法:
public class Main {
/**
* @param args
*/
public static void main(String[] args) {
Thread1 t1 = new Thread1("小黑线程-最低优先级");
Thread2 t2 = new Thread2("小红线程-最高优先级");
// 改变线程的优先级
// 线程的优先级只要知道就可以了,用默认的5级就足够,千万不要改变线程的优先级
t1.setPriority(Thread.MIN_PRIORITY);
t2.setPriority(10);
t1.start();
t2.start();
}
}
线程1:
public class Thread1 extends Thread {
public Thread1(String name) {
super(name);
}
// 我们把线程要执行的操作写在run方法中
// run()方法又被称为线程体
// 取得优先级并显示
// 优先级越高被线程调度器选中执行的机会越高
public void run() {
for (int i = 1; i <= 100000; i++) {
System.out.println(this.getName() + "在计数:i=" + i + ",线程的优先级是" + this.getPriority());
}
}
}
线程2:
public class Thread2 extends Thread {
public Thread2(String name) {
super(name);
}
// 我们把线程要执行的操作写在run方法中
// run()方法又被称为线程体
public void run() {
for (int i = 1; i <= 100000; i++) {
System.err.println(this.getName() + "在计数:i=" + i + ",线程的优先级是"
+ this.getPriority());
// =====================================================
// 注意:线程的sleep()/yield()方法都是静态方法.静态方法调用的时候和对象无关
// 暂停当前正在执行的线程.该线程重新回到了就绪队列,又有可能被线程调度器重新选中
// 即使这样的话,相对于其他的线程来说,也是让其他的线程更多的获得了运行机会
Thread.yield();
}
}
}
上面代码可以执行测试Thread.yield();让其他的线程更多的获得了运行机会。Thread.sleep(5000);// 在哪个线程中调用类线程的sleep(),哪个线程就会睡觉
任意一个项目
main方法:
public class Main {
/**
* @param args
*/
public static void main(String[] args) {
Thread1 t1 = new Thread1("小黑线程-最低优先级");
Thread2 t2 = new Thread2("小红线程-最高优先级");
try {
// 注意:线程的sleep()/yield()方法都是静态方法.静态方法调用的时候和对象无关
// 在哪个线程中调用类线程的sleep(),哪个线程就会睡觉
// 下面的代码是在主线程中调用了线程的sleep(),则主线程睡觉
t1.sleep(10000);
} catch (InterruptedException e) {
e.printStackTrace();
}
// 改变线程的优先级
// 线程的优先级只要知道就可以了,用默认的5级就足够,千万不要改变线程的优先级
t1.setPriority(Thread.MIN_PRIORITY);
t2.setPriority(10);
t1.start();
t2.start();
}
}
线程1:
public class Thread1 extends Thread {
public Thread1(String name) {
super(name);
}
// 我们把线程要执行的操作写在run方法中
// run()方法又被称为线程体
// 取得优先级并显示
// 优先级越高被线程调度器选中执行的机会越高
public void run() {
for (int i = 1; i <= 100000; i++) {
System.out.println(this.getName() + "在计数:i=" + i + ",线程的优先级是" + this.getPriority());
}
}
}
线程2:
public class Thread2 extends Thread {
public Thread2(String name) {
super(name);
}
// 我们把线程要执行的操作写在run方法中
// run()方法又被称为线程体
public void run() {
for (int i = 1; i <= 100000; i++) {
System.err.println(this.getName() + "在计数:i=" + i + ",线程的优先级是"
+ this.getPriority());
// =====================================================
// 注意:线程的sleep()/yield()方法都是静态方法.静态方法调用的时候和对象无关
// 在哪个线程中调用类线程的sleep(),哪个线程就会睡觉
try {
// 线程在睡觉期间不会回到就绪队列.该线程睡醒之后,才会重新回到就绪队列
Thread.sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
上面代码可以执行测试Thread.sleep(5000);//
在哪个线程中调用类线程的sleep(),哪个线程就会睡觉。
希望对你有帮助,祝你有一个好心情,加油!
若有错误、不全、可优化的点,欢迎纠正与补充;转载请注明出处!