threadexp5.java

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

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

public class ThreadExp5 {
        
    /**
     * Creates a new instance of <code>ThreadExp5</code>.
     */
    public ThreadExp5() {
    }
    
    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        // TODO code application logic here
     ComputerSum  computer=new ComputerSum();
     Thread  threadOne;
     threadOne=new Thread(computer);
     threadOne.setName("前部分");
     threadOne.start();
    }
}
class  ComputerSum  implements Runnable {
 int i=1,sum=0;
 public void run() {
  Thread  thread=Thread.currentThread();
  System.out.println(thread.getName()+"开始计算");
  while (i<=100) {
   sum+=i;
   System.out.print(" "+sum);
   if (i==50) {
   	System.out.println(thread.getName()+"完成任务了!i="+i);
   	Thread threadTwo=new Thread(this);
   	threadTwo.setName("后部分");
   	threadTwo.start();
   	i++;
   	return;
   }
   i++;
   try { 
   	Thread.sleep(300);
   }
   catch (InterruptedException e) {
   	System.out.println("there is interruption!"+e);
   }   
  }
 }
}

⌨️ 快捷键说明

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