首页 > 代码库 > JAVA 并发编程之线程管理2
JAVA 并发编程之线程管理2
今天,我们注重分析下Thread类。这个类有一些信息的属性。这些属性可以用来标识线程,显示线程的状态或者控制线程的优先级。
ID:保存了线程的唯一标识。
Name:保存了线程的名称
Priority:保存了线程的优先级。
Status:保存了线程的状态。java中线程的状态有:new,runnable,blocked,waiting,time waiting或者terminated.
现在,编写一个程序为10个线程指定名称和优先级,并且输出他们的状态信息直到线程结束。每个线程都将计算一个数字的乘法表。
1.创建一个名为Calculator的类,并实现了Runnable接口。
public class Calculator implements Runnable{}
2.声明一个名int类型私有变量number,并编写类的构造器,来为属性number设置值。
private int number;
public Calculator(int number){
this.number=number;
}
3.编写run方法。这个方法来执行我们创建线程的指令。
public void run(){
for(int i=1;i<=10;i++){
System.out.printf("%s: %d * %d =%d \n",Thread.currentThread().getName(),number,i,i*number);
}
}
4.编写一个名为Main的主类。同时创建main()方法。
public class Main {
public static void main(String[] args){}}
5.创建一个容量为10的线程数组,用来存储线程;创建一个容量为10的Thread.Status数组,以用来存放这10个线程运行的状态。
Thread threads[]=new Thread[10];
Thread.State status[]=new Thread.State[10];
6.创建一个容量为10的Calculator对象数组,为每个对象都设置不同的数字,然后用他们作为Thread构造器的参数来创建10个线程对象,并且将5个线程优先级设置为最高,另外5个线程优先级设置为最低。
for(int i=0;i<10;i++){
threads[i]=new Thread(new Calculator(i));
if((i%2)==0){
threads[i].setPriority(Thread.MAX_PRIORITY);
}else{
threads[i].setPriority(Thread.MIN_PRIORITY);
}
threads[i].setName("Thread"+i);
}
7.创建一个PrintWriter对象,用来把线程的状态演变写入到文件中。
try(FileWriter file=new FileWriter(".\\data\\log.txt");
PrintWriter pw=new PrintWriter(file);){
8.把这10个线程的状态写入到文件中,现有的线程状态都是NEW,
for(int i=0;i<10;i++){
pw.println("Main : Status of Thread"+i+":"
+threads[i].getState());
status[i]=threads[i].getState();
}
9.开始执行这个10个线程
for(int i=0;i<10;i++){
threads[i].start();
}
10.直到10个线程都运行完成,我们可以查看他们的状态。所有任务一个线程的状态发生了变化,我们就会讲它写入文件中
boolean finish=false;
while (!finish) {
for (int i=0; i<10; i++){
if (threads[i].getState()!=status[i]) {
writeThreadInfo(pw, threads[i],status[i]);
status[i]=threads[i].getState();
}
}
finish=true;
for(int i=0;i<10;i++){
finish=finish&&(threads[i].getState()==State.TERMINATED);
}
}
11.编写writerThreadInfo()方法,用来写下线成的ID,名称,优先级,旧的状态和新的状态。
private static void writeThreadInfo(PrintWriter pw, Thread thread, State state) {
pw.printf("Main : Id %d - %s\n",thread.getId(),thread.getName());
pw.printf("Main : Priority: %d\n",thread.getPriority());
pw.printf("Main : Old State: %s\n",state);
pw.printf("Main : New State: %s\n",thread.getState());
pw.printf("Main : ************************************\n");
}
12.运行结果:
Main : Status of Thread0:NEW
Main : Status of Thread1:NEW
Main : Status of Thread2:NEW
Main : Status of Thread3:NEW
Main : Status of Thread4:NEW
Main : Status of Thread5:NEW
Main : Status of Thread6:NEW
Main : Status of Thread7:NEW
Main : Status of Thread8:NEW
Main : Status of Thread9:NEW
Main : Id 9 - Thread0
Main : Priority: 10
Main : Old State: NEW
Main : New State: BLOCKED
Main : ************************************
Main : Id 10 - Thread1
Main : Priority: 1
Main : Old State: NEW
Main : New State: BLOCKED
Main : ************************************
Main : Id 11 - Thread2
Main : Priority: 10
Main : Old State: NEW
Main : New State: BLOCKED
Main : ************************************
Main : Id 12 - Thread3
Main : Priority: 1
Main : Old State: NEW
Main : New State: BLOCKED
Main : ************************************
Main : Id 13 - Thread4
Main : Priority: 10
Main : Old State: NEW
Main : New State: RUNNABLE
Main : ************************************
Main : Id 14 - Thread5
Main : Priority: 1
Main : Old State: NEW
Main : New State: BLOCKED
Main : ************************************
Main : Id 15 - Thread6
Main : Priority: 10
Main : Old State: NEW
Main : New State: BLOCKED
Main : ************************************
Main : Id 16 - Thread7
Main : Priority: 1
Main : Old State: NEW
Main : New State: BLOCKED
Main : ************************************
Main : Id 17 - Thread8
Main : Priority: 10
Main : Old State: NEW
Main : New State: BLOCKED
Main : ************************************
Main : Id 18 - Thread9
Main : Priority: 1
Main : Old State: NEW
Main : New State: BLOCKED
Main : ************************************
Main : Id 10 - Thread1
Main : Priority: 1
Main : Old State: BLOCKED
Main : New State: RUNNABLE
Main : ************************************
Main : Id 13 - Thread4
Main : Priority: 10
Main : Old State: RUNNABLE
Main : New State: BLOCKED
Main : ************************************
Main : Id 9 - Thread0
Main : Priority: 10
Main : Old State: BLOCKED
Main : New State: BLOCKED
Main : ************************************
Main : Id 13 - Thread4
Main : Priority: 10
Main : Old State: RUNNABLE
Main : New State: TERMINATED
Main : ************************************
Main : Id 14 - Thread5
Main : Priority: 1
Main : Old State: BLOCKED
Main : New State: BLOCKED
Main : ************************************
Main : Id 12 - Thread3
Main : Priority: 1
Main : Old State: BLOCKED
Main : New State: BLOCKED
Main : ************************************
Main : Id 11 - Thread2
Main : Priority: 10
Main : Old State: BLOCKED
Main : New State: RUNNABLE
Main : ************************************
Main : Id 11 - Thread2
Main : Priority: 10
Main : Old State: RUNNABLE
Main : New State: TERMINATED
Main : ************************************
Main : Id 17 - Thread8
Main : Priority: 10
Main : Old State: BLOCKED
Main : New State: BLOCKED
Main : ************************************
Main : Id 18 - Thread9
Main : Priority: 1
Main : Old State: BLOCKED
Main : New State: BLOCKED
Main : ************************************
Main : Id 16 - Thread7
Main : Priority: 1
Main : Old State: BLOCKED
Main : New State: BLOCKED
Main : ************************************
Main : Id 18 - Thread9
Main : Priority: 1
Main : Old State: BLOCKED
Main : New State: BLOCKED
Main : ************************************
Main : Id 17 - Thread8
Main : Priority: 10
Main : Old State: BLOCKED
Main : New State: RUNNABLE
Main : ************************************
Main : Id 17 - Thread8
Main : Priority: 10
Main : Old State: RUNNABLE
Main : New State: TERMINATED
Main : ************************************
Main : Id 12 - Thread3
Main : Priority: 1
Main : Old State: BLOCKED
Main : New State: RUNNABLE
Main : ************************************
Main : Id 12 - Thread3
Main : Priority: 1
Main : Old State: RUNNABLE
Main : New State: TERMINATED
Main : ************************************
Main : Id 14 - Thread5
Main : Priority: 1
Main : Old State: BLOCKED
Main : New State: BLOCKED
Main : ************************************
Main : Id 9 - Thread0
Main : Priority: 10
Main : Old State: BLOCKED
Main : New State: RUNNABLE
Main : ************************************
Main : Id 9 - Thread0
Main : Priority: 10
Main : Old State: RUNNABLE
Main : New State: TERMINATED
Main : ************************************
Main : Id 10 - Thread1
Main : Priority: 1
Main : Old State: BLOCKED
Main : New State: RUNNABLE
Main : ************************************
Main : Id 15 - Thread6
Main : Priority: 10
Main : Old State: BLOCKED
Main : New State: RUNNABLE
Main : ************************************
Main : Id 10 - Thread1
Main : Priority: 1
Main : Old State: RUNNABLE
Main : New State: BLOCKED
Main : ************************************
Main : Id 14 - Thread5
Main : Priority: 1
Main : Old State: BLOCKED
Main : New State: RUNNABLE
Main : ************************************
Main : Id 15 - Thread6
Main : Priority: 10
Main : Old State: RUNNABLE
Main : New State: BLOCKED
Main : ************************************
Main : Id 18 - Thread9
Main : Priority: 1
Main : Old State: BLOCKED
Main : New State: BLOCKED
Main : ************************************
Main : Id 14 - Thread5
Main : Priority: 1
Main : Old State: RUNNABLE
Main : New State: BLOCKED
Main : ************************************
Main : Id 18 - Thread9
Main : Priority: 1
Main : Old State: BLOCKED
Main : New State: BLOCKED
Main : ************************************
Main : Id 14 - Thread5
Main : Priority: 1
Main : Old State: BLOCKED
Main : New State: RUNNABLE
Main : ************************************
Main : Id 15 - Thread6
Main : Priority: 10
Main : Old State: BLOCKED
Main : New State: BLOCKED
Main : ************************************
Main : Id 10 - Thread1
Main : Priority: 1
Main : Old State: BLOCKED
Main : New State: RUNNABLE
Main : ************************************
Main : Id 15 - Thread6
Main : Priority: 10
Main : Old State: BLOCKED
Main : New State: RUNNABLE
Main : ************************************
Main : Id 15 - Thread6
Main : Priority: 10
Main : Old State: RUNNABLE
Main : New State: TERMINATED
Main : ************************************
Main : Id 14 - Thread5
Main : Priority: 1
Main : Old State: BLOCKED
Main : New State: RUNNABLE
Main : ************************************
Main : Id 14 - Thread5
Main : Priority: 1
Main : Old State: RUNNABLE
Main : New State: TERMINATED
Main : ************************************
Main : Id 18 - Thread9
Main : Priority: 1
Main : Old State: BLOCKED
Main : New State: RUNNABLE
Main : ************************************
Main : Id 18 - Thread9
Main : Priority: 1
Main : Old State: RUNNABLE
Main : New State: TERMINATED
Main : ************************************
Main : Id 16 - Thread7
Main : Priority: 1
Main : Old State: BLOCKED
Main : New State: RUNNABLE
Main : ************************************
Main : Id 16 - Thread7
Main : Priority: 1
Main : Old State: RUNNABLE
Main : New State: TERMINATED
Main : ************************************
Main : Id 10 - Thread1
Main : Priority: 1
Main : Old State: BLOCKED
Main : New State: RUNNABLE
Main : ************************************
JAVA 并发编程之线程管理2