模拟接力赛跑

 1 package demo7;
 2 
 3 //模拟接力赛跑,5个人每人跑200米,规定每人跑100米后换下个选手,每跑10米显示信息
 4 public class MyThread6 implements Runnable{
 5     private int count;
 6     
 7     public void run() {
 8         int i=0;
 9         while(i<2) {
10             running();
11             try {
12                 Thread.sleep(500);
13             } catch (InterruptedException e) {
14                 e.printStackTrace();
15             }
16             i++;
17         }
18     }
19     
20     public synchronized void running() {
21         
22         System.out.println(Thread.currentThread().getName()+"拿到接力棒!");
23             for (int j = 0; j <10; j++) {
24                 count+=10;
25                 try {
26                     Thread.sleep(200);
27                 } catch (InterruptedException e) {
28                     e.printStackTrace();
29                 }
30                 System.out.println("跑"+count+"米的选手是---"+Thread.currentThread().getName());
31             }
32         
33         
34     }
35 }
 1 package demo7;
 2 
 3 public class Test6 {
 4     public static void main(String[] args) {
 5         MyThread6 mt = new MyThread6();
 6         
 7         Thread t1 = new Thread(mt,"1号");
 8         Thread t2 = new Thread(mt,"2号");
 9         Thread t3 = new Thread(mt,"3号");
10         Thread t4 = new Thread(mt,"4号");
11         Thread t5 = new Thread(mt,"5号");
12         t1.start();
13         t2.start();
14         t3.start();
15         t4.start();
16         t5.start();
17 
18 
19     }
20 }

 

转载于:https://www.cnblogs.com/baichang/p/10205740.html

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值