0
点赞
收藏
分享

微信扫一扫

新建双线 程


public class Demo2 {

public static void main(String[] args) {

//自定义线程
new Thread(new Runnable() {
@Override
public void run() {
for (int i = 0; i < 100; i++) {
System.out.println(Thread.currentThread().getName() + "---"
+ i);
}
}

}).start();

//main线程
for (int i = 0; i < 100; i++) {
System.out.println(Thread.currentThread().getName() + "---" + i);
}

//把main线程放在下面不能交替出现
}

}

和上面都是一样的,都是用接口

class Ticket implements Runnable {
@Override
public void run() {
for (int i = 0; i < 100; i++) {
System.out.println(Thread.currentThread().getName() + i);

}

}

}

public class Demo3 {
public static void main(String[] args) {
Ticket ticket = new Ticket();
Thread thread = new Thread(ticket, "自定义线程");
thread.start();

for (int i = 0; i < 100; i++) {
System.out.println(Thread.currentThread().getName() + i);
}

}
}






举报

相关推荐

0 条评论