Java -- Runnable

来源:互联网 发布:mac微信怎么视频 编辑:程序博客网 时间:2024/05/22 13:13
//创建多线程的方式二:通过实现的方式
class PrintNum1 implements Runnable{


public void run() {
for (int i = 0; i <= 100; i++) {
if(i % 2 == 0){
System.out.println(Thread.currentThread().getName() + ":" + i);
}

}

}

}


public class Test_Thread2 {
public static void main(String[] args) {
PrintNum1 p = new PrintNum1();
//要想启动一个多线程必须调用start()
Thread t1 = new Thread(p);
t1.start(); //启动线程:执行Thread对象生产时构造器形参的对象的run()方法
Thread t2 = new Thread(p);
t2.start();

}
}
原创粉丝点击