eclipse(英文版)入门:[7]创建线程的两种方法
1、启动eclipse,新建一个工程,在工程中新建一个类(具体看参考资料)
2、【方法一】将以下代码复制到eclipse中:
class Xc extends Thread {
public void run() {
for(int i=0; i<20; i++) {
System.out.println("子函数");
}
}
}
public class test {
public static void main(String[] args) {
Xc xc = new Xc();
//xc.run();
xc.start();
for(int i=0; i<20; i++) {
System.out.println("主函数");
}
}
}
3、点击运行
4、【方法二】将以下代码复制到eclipse中:
public class test4 {
public static void main(String[] args) {
Thread xc1 = new Thread(new Xc41());
Thread xc2 = new Thread(new Xc42());
xc1.start();
xc2.start();
}
}
class Xc41 implements Runnable {
public void run() {
for(int i=0; i<100; i++) {
System.out.println("1线程" + i);
}
}
}
class Xc42 implements Runnable {
public void run() {
for(int i=0; i<100; i++) {
System.out.println("第二个线程正在被执行");
}
}
}
5、点击运行