Java并发学习之二——获取和设置线程信息

Thread类的对象中保存了一些属性信息能够帮助我们辨别每一个线程,知道它的一些信息

ID:每个线程的独特标示;

Name:线程的名称;

Priority:线程对象的优先级。优先级别在1-10之间,1是最低级,10是最高级。

Status:线程状态。在Java中,线程只有6种状态:new,runnable,blocked,waiting,time waiting terminated

            java线程的几种状态:java thread的运行周期中, 有几种状态, 在 java.lang.Thread.State 中有详细定义和说明:

            1.NEW 状态是指线程刚创建, 尚未启动

            2.RUNNABLE 状态是线程正在正常运行中, 当然可能会有某种耗时计算/IO等待的操作/CPU时间片切换等, 这个状态下发生的等待一般是其他系统资源, 而不是锁, Sleep等

            3.BLOCKED  这个状态下, 是在多个线程有同步操作的场景, 比如正在等待另一个线程的synchronized 块的执行释放, 或者可重入的 synchronized块里别人调用wait() 方法, 也就是这里是线程在等待进入临界区

            4.WAITING  这个状态下是指线程拥有了某个锁之后, 调用了他的wait方法, 等待其他线程/锁拥有者调用 notify / notifyAll 一遍该线程可以继续下一步操作, 这里要区分 BLOCKED 和 WATING 的区别, 一个是在临界点外面等待进入, 一个是在理解点里面wait等待别人notify, 线程调用了join方法 join了另外的线程的时候, 也会进入WAITING状态, 等待被他join的线程执行结束

            5.TIMED_WAITING  这个状态就是有限的(时间限制)的WAITING, 一般出现在调用wait(long), join(long)等情况下, 另外一个线程sleep后, 也会进入TIMED_WAITING状态

            6.TERMINATED 这个状态下表示 该线程的run方法已经执行完毕了, 基本上就等于死亡了(当时如果线程被持久持有, 可能不会被回收)

现在写一个程序,将线程的信息保存到文件中方便查看;

package chapter;  
  
public class Calculator2 implements Runnable{  
  
    private int number;  
    public Calculator2(int number){  
        this.number = number;  
    }  
      
    @Override  
    public void run() {  
        // TODO Auto-generated method stub  
        for (int i = 0; i < 10; i++) {  
            System.out.printf("%s:%d*%d = %d\n",Thread.currentThread().getName(),number,i,i*number);  
        }  
    }  
}  
package chapter;  
import java.io.FileWriter;  
import java.io.PrintWriter;  
import java.lang.Thread.State;  
  
public class Main2 {  
  
    /** 
     * <p> 
     * </p> 
     * @author zhangjunshuai 
     * @date 2014-8-8 下午3:36:20 
     * @param args 
     */  
    public static void main(String[] args) {  
        // TODO Auto-generated method stub  
  
        Thread[] threads = new Thread[10];  
        Thread.State[] status = new Thread.State[10];  
        for (int i = 0; i < 10; i++) {  
            threads[i] = new Thread(new Calculator2(i));  
            if((i%2) == 0)  
                threads[i].setPriority(Thread.MAX_PRIORITY);  
            else  
                threads[i].setPriority(Thread.MIN_PRIORITY);  
            threads[i].setName("Thread"+i);  
        }  
        try{  
            FileWriter file = new FileWriter(".\\xianchenglog1.txt");  
            PrintWriter pw = new PrintWriter(file);  
            for(int i =0;i<10;i++){  
                pw.println("main: status of thread "+i+": "+threads[i].getState());  
                status[i] = threads[i].getState();  
            }  
              
            for(int i =0;i<10;i++){  
                threads[i].start();  
            }  
            boolean finish = false;  
            while(!finish){  
                for (int i = 0; i < 10; i++) {  
                    if(threads[i].getState()!=status[i]){  
                        writeThreadInfo(pw,threads[i],status[i]);  
                    }  
                }  
                finish = true;  
            }  
            pw.close();  
        }catch(Exception e){  
              
        }  
          
          
    }  
    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");  
          
        }  
  
  
}  

Thread0:0*0 = 0
Thread0:0*1 = 0
Thread0:0*2 = 0
Thread0:0*3 = 0
Thread3:3*0 = 0
Thread2:2*0 = 0
Thread2:2*1 = 2
Thread2:2*2 = 4
Thread2:2*3 = 6
Thread2:2*4 = 8
Thread2:2*5 = 10
Thread2:2*6 = 12
Thread2:2*7 = 14
Thread1:1*0 = 0
Thread8:8*0 = 0
Thread4:4*0 = 0
Thread4:4*1 = 4
Thread4:4*2 = 8
Thread5:5*0 = 0
Thread7:7*0 = 0
Thread9:9*0 = 0
Thread9:9*1 = 9
Thread6:6*0 = 0
Thread9:9*2 = 18
Thread7:7*1 = 7
Thread5:5*1 = 5
Thread4:4*3 = 12
Thread8:8*1 = 8
Thread1:1*1 = 1
Thread2:2*8 = 16
Thread2:2*9 = 18
Thread3:3*1 = 3
Thread0:0*4 = 0
Thread3:3*2 = 6
Thread1:1*2 = 2
Thread8:8*2 = 16
Thread8:8*3 = 24
Thread4:4*4 = 16
Thread4:4*5 = 20
Thread4:4*6 = 24
Thread4:4*7 = 28
Thread4:4*8 = 32
Thread4:4*9 = 36
Thread5:5*2 = 10
Thread7:7*2 = 14
Thread9:9*3 = 27
Thread6:6*1 = 6
Thread6:6*2 = 12
Thread6:6*3 = 18
Thread6:6*4 = 24
Thread6:6*5 = 30
Thread6:6*6 = 36
Thread6:6*7 = 42
Thread6:6*8 = 48
Thread6:6*9 = 54
Thread9:9*4 = 36
Thread7:7*3 = 21
Thread5:5*3 = 15
Thread5:5*4 = 20
Thread8:8*4 = 32
Thread8:8*5 = 40
Thread8:8*6 = 48
Thread8:8*7 = 56
Thread8:8*8 = 64
Thread8:8*9 = 72
Thread1:1*3 = 3
Thread3:3*3 = 9
Thread0:0*5 = 0
Thread3:3*4 = 12
Thread3:3*5 = 15
Thread1:1*4 = 4
Thread5:5*5 = 25
Thread7:7*4 = 28
Thread9:9*5 = 45
Thread7:7*5 = 35
Thread5:5*6 = 30
Thread1:1*5 = 5
Thread3:3*6 = 18
Thread0:0*6 = 0
Thread3:3*7 = 21
Thread1:1*6 = 6
Thread5:5*7 = 35
Thread7:7*6 = 42
Thread9:9*6 = 54
Thread7:7*7 = 49
Thread5:5*8 = 40
Thread1:1*7 = 7
Thread1:1*8 = 8
Thread1:1*9 = 9
Thread3:3*8 = 24
Thread0:0*7 = 0
Thread0:0*8 = 0
Thread0:0*9 = 0
Thread3:3*9 = 27
Thread5:5*9 = 45
Thread7:7*8 = 56
Thread9:9*7 = 63
Thread7:7*9 = 63
Thread9:9*8 = 72
Thread9:9*9 = 81

 

posted @ 2017-07-25 17:02  十月围城小童鞋  阅读(169)  评论(0)    收藏  举报