关于Java多线程技术的问题,请注意,一个是2个程序
为什么上面的程序运行的结果是随机的,而下面的程序运行的结果却是固定不变的publicclassDoubleThread{publicstaticvoidmain(Stri...
为什么上面的程序运行的结果是随机的,而下面的程序运行的结果却是固定不变的
public class DoubleThread {
public static void main(String[] args) {
Thread t1 = new Thread() {
public void run() {
for (int i =1; i <=10; i++) {
System.out.println(i);
}
}
};
Thread t2 = new Thread() {
public void run() {
for (int i =11; i <=20; i++) {
System.out.println(i);
}
}
};
t1.start();
t2.start();
}
}
public class hello extends Thread {
public hello(String name) {
this.name = name;
}
public void run() {
for (int i = 0; i < 5; i++) {
System.out.println(name + "运行 " + i);
}
}
public static void main(String[] args) {
hello h1=new hello("A");
hello h2=new hello("B");
h1.run();
h2.run();
}
private String name;
} 展开
public class DoubleThread {
public static void main(String[] args) {
Thread t1 = new Thread() {
public void run() {
for (int i =1; i <=10; i++) {
System.out.println(i);
}
}
};
Thread t2 = new Thread() {
public void run() {
for (int i =11; i <=20; i++) {
System.out.println(i);
}
}
};
t1.start();
t2.start();
}
}
public class hello extends Thread {
public hello(String name) {
this.name = name;
}
public void run() {
for (int i = 0; i < 5; i++) {
System.out.println(name + "运行 " + i);
}
}
public static void main(String[] args) {
hello h1=new hello("A");
hello h2=new hello("B");
h1.run();
h2.run();
}
private String name;
} 展开
1个回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询