⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 threadexp1.java

📁 java关于多线程的课堂练习
💻 JAVA
字号:
/**
 * @(#)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 + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -