【Java开源代码栏目提醒】:网学会员为广大网友收集整理了,TextPriorityLevel.java,希望对大家有所帮助!
package com.zf.s14; //创建一个包
class OneFamilyThread implements Runnable { // 线程实现接口Runnable
Thread thread; //声明一个线程
OneFamilyThread(){} //默认构造方法
OneFamilyThread(String name) { //构造方法初始一个线程
thread = new Thread(this, name);
}
public void run() {
System.out.println("家族人员:"+thread.getName()); //获得线程的名称
}
public void startThreadByLevel(){ //按优先级执行线程
OneFamilyThread o1=new OneFamilyThread("爷爷");//创建线程对象并命名
OneFamilyThread o2= new OneFamilyThread("奶奶");
OneFamilyThread o3 = new OneFamilyThread("爸爸");
OneFamilyThread o4 = new OneFamilyThread("妈妈");
OneFamilyThread o5 = new OneFamilyThread("我");
o1.thread.setPriority(Thread.MAX_PRIORITY); //MAX_PRIORITY优先级最大
o2.thread.setPriority(Thread.MAX_PRIORITY - 1); //较次之
o3.thread.setPriority(Thread.NORM_PRIORITY); //NORM_PRIORITY处于中间位置
o4.thread.setPriority(Thread.NORM_PRIORITY - 1);
o5.thread.setPriority(Thread.MIN_PRIORITY); //MIN_PRIORITY优先级最小
o1.thread.start(); //启动线程
o2.thread.start();
o3.thread.start();
o4.thread.start();
o5.thread.start();
try {
o5.thread.join(); //等待线程运行结束
} catch (InterruptedException e) { //捕获拦截异常
System.out.println("等待线程结束出错:"+e.getMessage());
}
}
}
class TwoFamilyThread implements Runnable {
Thread thread;
public TwoFamilyThread() {} //默认构造方法
public void run() {
System.out.println("家族人员:"+thread.getName()); //获得线程的名称
}
public void startThreadByLevel(){ //按优先级执行线程
TwoFamilyThread t1 = new TwoFamilyThread(); //创建线程类
TwoFamilyThread t2 = new TwoFamilyThread();
TwoFamilyThread t3 = new TwoFamilyThread();
TwoFamilyThread t4 = new TwoFamilyThread();
TwoFamilyThread t5 = new TwoFamilyThread();
t1.thread = new Thread(t1, "爷爷"); //线程类赋名称
t2.thread = new Thread(t2, "奶奶");
t3.thread = new Thread(t3, "爸爸");
t4.thread = new Thread(t4, "妈妈");
t5.thread = new Thread(t5, "我");
t1.thread.setPriority(10); //线程优先级10为优先级最高 t2.thread.setPriority(9);
t3.thread.setPriority(8);
t4.thread.setPriority(7);
t5.thread.setPriority(6);
t1.thread.start(); //启动线程
t2.thread.start();
t3.thread.start();
t4.thread.start();
t5.thread.start();
try {
t5.thread.join(); //等待线程运行结束
} catch (InterruptedException e) { //捕获拦截异常
System.out.println("等待线程结束出错:"+e.getMessage());
}
}
}
class ThreeFamilyThread extends Thread {
Thread thread;
public ThreeFamilyThread(){} //默认构造方法
public ThreeFamilyThread(String name) { //带参数构造方法初始一个线程
thread = new Thread(this, name);
}
public void run() {
System.out.println("家族人员:"+thread.getName()); //获得线程的名称
}
public void startThreadByLevel(){ //按优先级执行线程
ThreeFamilyThread e1=new ThreeFamilyThread("爷爷");//创建线程对象并命名
ThreeFamilyThread e2 = new ThreeFamilyThread("奶奶");
ThreeFamilyThread e3 = new ThreeFamilyThread("爸爸");
ThreeFamilyThread e4 = new ThreeFamilyThread("妈妈");
ThreeFamilyThread e5 = new ThreeFamilyThread("我");
e1.thread.setPriority(Thread.MAX_PRIORITY); //MAX_PRIORITY优先级最大
e2.thread.setPriority(Thread.MAX_PRIORITY - 1); //较次之
e3.thread.setPriority(Thread.NORM_PRIORITY); //NORM_PRIORITY处于中间位置
e4.thread.setPriority(Thread.NORM_PRIORITY - 1);
e5.thread.setPriority(Thread.MIN_PRIORITY); //MIN_PRIORITY优先级最小
e1.thread.start(); //启动线程
e2.thread.start();
e3.thread.start();
e4.thread.start();
e5.thread.start();
try {
e5.thread.join(); //等待线程运行结束
} catch (InterruptedException e) { //捕获拦截异常
System.out.println("等待线程结束出错:"+e.getMessage());
}
}
}
public class TextPriorityLevel extends Thread { //操作运用优先级实现家庭等的类
public static void main(String []args){ //java程序主入口处
System.out.println("继承Thread类,根据数字从高到低实现线程优先级");
new TwoFamilyThread().startThreadByLevel();
System.out.println("继承Thread类,根据静态等级常量实现线程优先级");
new ThreeFamilyThread().startThreadByLevel();
System.out.println("实现Runnable接口,根据静态等级常量实现线程优先级");
new OneFamilyThread().startThreadByLevel();
}
}
上一篇:
TextPrintPicture.java
下一篇:
php论坛毕业论文