📄 threadexp5.java
字号:
/**
* @(#)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 + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -