testthread.java

来自「本书是一本为Java学习者在基础内容学习结束后进行课程设计时提供参考的指导书」· Java 代码 · 共 65 行

JAVA
65
字号
package apibook.c2.s3;public class TestThread {  public static void main(String[] args) {    Thread r=null;    for (int i=0;i<5 ;i++ ) {      r=new ct(i);      r.start();    }  }}class ct extends Thread{  private int k;  public ct(int j){    k=j;  }  public void run(){    for (int i=0;i<100;i++ ) {      synchronized(System.out){          System.out.println(Thread.currentThread().getName());          System.out.println(Thread.activeCount());          this.setPriority(3);          //System.out.println(Thread.currentThread().getPriority());      }          try {            Thread t=null;            for(int n=0;n<3;n++){              t=new ctt(k,n);              t.setPriority(5);              t.join();              t.start();            }            //this.wait();            //this.notifyAll();            this.sleep(50);          }          catch (Exception ex) {          }    }  }}class ctt extends Thread{  private int k,m;  public ctt(int l,int j){    k=l;    m=j;  }  public void run(){    for (int i=0;i<100 ;i++ ) {      synchronized(System.out){          System.out.println("child Thread:"+k+"-"+m);      }          try {            //this.wait();            //this.notifyAll();            this.sleep(50);          }          catch (Exception ex) {          }    }  }}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?