("线程 " + number + ":计数 " + count);
if(++count== 6) return;
}
}
这个return是不是应该换成break;
因为return是返回的意思搞反了吧:- public class MyThread extends Thread{
- private static Runnable i;
- private int count = 1, number;
- public MyThread(int num){
- number = num;
- System.out.println("创建线程 " + number);
- }
- public void run() {
- while(true) {
- System.out.print("线程 " + number + ":计数 " + count);
- count++;
- if(count==6){
- System.out.println();
- break ;
- }
- }
- }
- public static void main(String args[]) {
- for(int i = 1 ;i<5;i++){
- try {
- Thread.sleep(10);
- } catch (InterruptedException e) {
-
- e.printStackTrace();
- }
- new MyThread(i).start();
-
- }
-
- }
- }
复制代码
把我的抄一遍看看(注意不要复制)
public class MyThread extends Thread{
private static Runnable i;
private int count = 1, number;
public MyThread(int num){
number = num;
System.out.println("创建线程 " + number);
}
public void run() {
while(true) {
System.out.print("线程 " + number + ":计数 " + count);
count++;
if(count==6){
System.out.println();
break ;
}
}
}
public static void main(String args[]) {
for(int i = 1 ;i<5;i++){
try {
Thread.sleep(10);
} catch (InterruptedException e) {
e.printStackTrace();
}
new MyThread(i).start();
}
}
}
1我给你的程序加了一个休眠;
2把程序的new MyThread(i+1)改成了MyThread(i);[code]public class MyThread extends Thread{
private static Runnable i;
private int count = 1, number;
public MyThread(int num){
number = num;
System.out.println("创建线程 " + number);
}
public void run() {
while(true) {
System.out.print("线程 " + number + ":计数 " + count);
count++;
if(count==6){
System.out.println();
break ;
}
}
}
public static void main(String args[]) {
for(int i = 1 ;i<5;i++){
try {
Thread.sleep(10);
} catch (InterruptedException e) {
e.printStackTrace();
}
new MyThread(i).start();
}
}
}
就是在退出的时候换成了break在退出前又加了一个换行效果如图 |
|