setPriority()方法
实例方法:void setPriority(int newPriority); //设置线程的优先级
int getPriority(); //获取先序的优先级
MAX_PRIORITY 10
NORM_PRIORITY 5
MIN_PRIORITY 1
一般的线程优先级是5,为默认
例如:
public class Main1{
public static void main(String[] args) {
System.out.println("最高优先级"+Thread.MAX_PRIORITY);
System.out.println("最低优先级"+Thread.MIN_PRIORITY);
System.out.println("默认优先级"+Thread.NORM_PRIORITY);
System.out.println(Thread.currentThread().getName()+"线程优先级:"+Thread.currentThread().getPriority());
Thread t=new Thread(new thread1());
t.setName("t1");
t.start();
for(int i=0;i<5;i++)
System.out.println(Thread.currentThread().getName()+"-->"+i);
}
}
class thread1 implements Runnable{
@Override
public void run() {
System.out.println(Thread.currentThread().getName()+"线程优先级:"+Thread.currentThread().getPriority());
for(int i=0;i<5;i++)
System.out.println(Thread.currentThread().getName()+"-->"+i);
}
}
最高优先级10
最低优先级1
默认优先级5
main线程优先级:5
main–>0
main–>1
main–>2
t1线程优先级:5
main–>3
t1–>0
t1–>1
t1–>2
t1–>3
t1–>4
main–>4
更改优先级使用setPriority()方法,实例如下:
public class Main1{
public static void main(String[] args) {
System.out.println("最高优先级"+Thread.MAX_PRIORITY);
System.out.println("最低优先级"+Thread.MIN_PRIORITY);
System.out.println("默认优先级"+Thread.NORM_PRIORITY);
//把主线程的优先级设为1
Thread.currentThread().setPriority(1);
System.out.println(Thread.currentThread().getName()+"线程优先级:"+Thread.currentThread().getPriority());
//把t线程的优先级设为10
Thread t=new Thread(new thread1());
t.setName("t1");
t.setPriority(10);
t.start();
for(int i=0;i<5;i++)
System.out.println(Thread.currentThread().getName()+"-->"+i);
}
}
class thread1 implements Runnable{
@Override
public void run() {
System.out.println(Thread.currentThread().getName()+"线程优先级:"+Thread.currentThread().getPriority());
for(int i=0;i<5;i++)
System.out.println(Thread.currentThread().getName()+"-->"+i);
}
}
最高优先级10
最低优先级1
默认优先级5
main线程优先级:1
main–>0
main–>1
main–>2
main–>3
main–>4
t1线程优先级:10
t1–>0
t1–>1
t1–>2
t1–>3
t1–>4
优先级较高的获取CPU时间片的可能更大些
yield()方法
静态方法:static void yield()
Thread.yield();
暂停当前正在执行的线程,并执行其他线程
yield()方法的执行会让当前线程从“执行状态”回到“就绪状态”
public class Main1{
public static void main(String[] args) {
Thread t=new Thread(new thread1());
t.setName("t1");
t.start();
for(int i=1;i<=100;i++)
System.out.println(Thread.currentThread().getName()+"-->"+i);
}
}
class thread1 implements Runnable{
@Override
public void run() {
for(int i=1;i<=100;i++){
if(i%10==0){
Thread.yield();
}
System.out.println(Thread.currentThread().getName()+"-->"+i);
}
}
}
jion()方法
join() //合并线程
使当前线程受阻,执行t线程,t线程执行完后,当前线程继续执行
public class Main1{
public static void main(String[] args) throws InterruptedException {
System.out.println("start");
Thread t=new Thread(new thread1());
t.setName("t1");
t.start();
//当前线程受阻,执行t线程,t线程执行完后,当前线程继续执行
t.join();
System.out.println("over");
}
}
class thread1 implements Runnable{
@Override
public void run() {
for(int i=0;i<=10;i++){
System.out.println(Thread.currentThread().getName()+"-->"+i);
}
}
}
start
t1–>0
t1–>1
t1–>2
t1–>3
t1–>4
t1–>5
t1–>6
t1–>7
t1–>8
t1–>9
t1–>10
over