package thread;
public class 线程优先级 {
public static void main(String[] args) {
// TODO Auto-generated method stub
//给线程命名
Tang tang = new Tang();
Thread tangTh = new Thread(tang);
tangTh.setName("煮汤线程");//给线程命名
System.out.println(tangTh.getName()+"的优先级是:"+tangTh.getPriority());
Cooking cooking = new Cooking();
Thread cookingTh = new Thread(cooking);
cookingTh.setName("炒菜线程");
System.out.println(cookingTh.getName()+"的优先级是:"+cookingTh.getPriority());
tangTh.setPriority(10);
cookingTh.setPriority(1);
tangTh.start();
cookingTh.start();
}
}
class Tang implements Runnable{
public void bao(){
for (int i = 1; i <= 4; i++) {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("枸杞老鸭汤,煲了"+i+"个小时了");
}
System.out.println("汤好了");
}
@Override
public void run() {
// TODO Auto-generated method stub
bao();
}
}
class Cooking implements Runnable{
public void cook(){
for (int i = 1; i <= 4; i++) {
try {
Thread.sleep(600);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("正在炒个"+i+"菜");
}
System.out.println("菜好了");
}
@Override
public void run() {
// TODO Auto-generated method stub
cook();
}
}
例子二
package thread;
public class 优先级使用2 {
public static void main(String[] args) {
// TODO Auto-generated method stub
Thread th1 = new Thread(new Print1());
Thread th2 = new Thread(new Print1());
Thread th3 = new Thread(new Print1());
th1.setPriority(10);
th2.setPriority(1);
th1.start();
th2.start();
th3.start();
}
}
//打印1-1000
class Print1 implements Runnable{
@Override
public void run() {
// TODO Auto-generated method stub
for (int i = 1; i < 1000; i++) {
System.out.print(i+"\t");
if(i%20 == 0)
System.out.println();
}
}
}
//打印1001-2000
class Print2 implements Runnable{
@Override
public void run() {
// TODO Auto-generated method stub
for (int i = 1001; i < 2000; i++) {
System.out.print(i+"\t");
if(i%20 == 0)
System.out.println();
}
}
}
//打印2001-3000
class Print3 implements Runnable{
@Override
public void run() {
// TODO Auto-generated method stub
for (int i = 2001; i < 3000; i++) {
System.out.print(i+"\t");
if(i%20 == 0)
System.out.println();
}
}
}
package thread;
public class 优先级使用2 {
public static void main(String[] args) {
// TODO Auto-generated method stub
Thread th1 = new Thread(new Print1());
Thread th2 = new Thread(new Print1());
Thread th3 = new Thread(new Print1());
th1.setPriority(10);
th2.setPriority(1);
th1.start();
th2.start();
th3.start();
}
}
//打印1-1000
class Print1 implements Runnable{
@Override
public void run() {
// TODO Auto-generated method stub
for (int i = 1; i < 1000; i++) {
System.out.print(i+"\t");
if(i%20 == 0)
System.out.println();
}
}
}
//打印1001-2000
class Print2 implements Runnable{
@Override
public void run() {
// TODO Auto-generated method stub
for (int i = 1001; i < 2000; i++) {
System.out.print(i+"\t");
if(i%20 == 0)
System.out.println();
}
}
}
//打印2001-3000
class Print3 implements Runnable{
@Override
public void run() {
// TODO Auto-generated method stub
for (int i = 2001; i < 3000; i++) {
System.out.print(i+"\t");
if(i%20 == 0)
System.out.println();
}
}
}