threadexp1.java

来自「java关于多线程的课堂练习」· Java 代码 · 共 50 行

JAVA
50
字号
/**
 * @(#)ThreadExp1.java
 *
 *
 * @author 
 * @version 1.00 2007/11/29
 */

public class ThreadExp1 {
        
    /**
     * Creates a new instance of <code>ThreadExp1</code>.
     */
    public ThreadExp1() {
    }
    
    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        // TODO code application logic here
     People  personOne,personTwo;
     StringBuffer  str=new StringBuffer();
     personOne=new People("John",str);
     personTwo=new People("Mary",str);
     personOne.start();
     personTwo.start();
    }
}
class People  extends  Thread {
 StringBuffer  str;
 People(String s,StringBuffer str) {
  setName(s);
  this.str=str;
 }
 public void run() {
  for (int i=1; i<=3; i++) {
   str.append(getName()+",");
   System.out.println("我是"+getName()+",字符串为:"+str);
   try {
   	sleep((int)(Math.random()*200));
   // sleep(200);
   }
   catch (InterruptedException e) {
   	System.out.println("there is interrupt occured!"+e);
   }
  }
 }
}

⌨️ 快捷键说明

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