package java_learning;
public class test_MyRunnable {
public static void main(String[]args){
for(int i=0; i<1; i++){
System.out.println("我是主线程"+ Thread.currentThread().getName()+" "+i);
Runnable myrunnable = new MyRunable(); // 创建一个Runnable实现类的对象
Thread thread1 = new Thread(myrunnable);// 将myRunnable作为Thread target创建新的线程
Thread thread2 = new Thread(myrunnable);
thread1.start();
thread2.start();
}
}
}
class MyRunable implements Runnable{
private int i = 0;
@Override
public void run() {
// TODO 自动生成的方法存根
for (i=0;i<1;i++){
System.out.println("我是子线程"+Thread.currentThread().getName()+" "+i);
}
}
}
/*实现Runnable接口,并重写该接口的run()方法,该run()方法同样是线程执行体,
* 创建Runnable实现类的实例,并以此实例作为Thread类的target来创建Thread对象,该Thread对象才是真正的线程对象。
*/
test_MyRunnable
最新推荐文章于 2022-05-04 21:29:58 发布