eclipse(英文版)入门:[7]创建线程的两种方法

2025-10-05 14:20:58

1、启动eclipse,新建一个工程,在工程中新建一个类(具体看参考资料)

eclipse(英文版)入门:[7]创建线程的两种方法

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("主函数");

}

}

}

eclipse(英文版)入门:[7]创建线程的两种方法

3、点击运行

eclipse(英文版)入门:[7]创建线程的两种方法

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("第二个线程正在被执行");

}

}

}

eclipse(英文版)入门:[7]创建线程的两种方法

5、点击运行

eclipse(英文版)入门:[7]创建线程的两种方法

声明:本网站引用、摘录或转载内容仅供网站访问者交流或参考,不代表本站立场,如存在版权或非法内容,请联系站长删除,联系邮箱:site.kefu@qq.com。
猜你喜欢