Runnable简单举列子

package cn.mb.thread;
/**

  • 共享资源,并发(线程安全)
  • @author lenovo

*/
public class Web12306 implements Runnable {
//票数
private int ticketNumbles=96;
@Override
public void run() {
while(true) {
if(ticketNumbles<0) {
break;
}
try {
Thread.sleep(200);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName()+"—>"+ticketNumbles–);
}
}
public static void main(String[] args) {
//一个资源
Web12306 web=new Web12306();
System.out.println(Thread.currentThread().getName());
//多个代理
new Thread(web,“mbb”).start();
new Thread(web,“mb”).start();
new Thread(web,“m”).start();

}

}
运行结果:
main
mb—>95
mbb—>94
m—>96
mbb—>93
m—>92
mb—>91
mbb—>90
m—>90
mb—>90
mbb—>89
m—>87
mb—>88
mbb—>86
m—>85
mb—>84
m—>83
mb—>82
mbb—>82
mbb—>81
m—>79
mb—>80
mbb—>78
mb—>77
m—>78
m—>76
mbb—>74
mb—>75
mb—>73
m—>71
mbb—>72
mb—>70
m—>68
mbb—>69
mb—>67
mbb—>66
m—>65
mb—>64
m—>63
mbb—>62
mb—>61
mbb—>59
m—>60
mb—>58
m—>57
mbb—>56
mb—>55
mbb—>54
m—>53
mb—>52
mbb—>50
m—>51
mb—>49
mbb—>48
m—>47
mb—>46
m—>45
mbb—>44
mb—>43
m—>42
mbb—>42
mb—>41
m—>40
mbb—>39
mb—>38
mbb—>37
m—>36
mb—>35
mbb—>34
m—>33
mb—>32
m—>31
mbb—>30
mb—>29
mbb—>28
m—>27
mb—>26
mbb—>25
m—>24
mb—>23
m—>22
mbb—>21
mb—>20
mbb—>19
m—>18
mb—>17
m—>16
mbb—>15
mb—>14
m—>12
mbb—>13
mb—>11
m—>10
mbb—>9
mb—>8
m—>7
mbb—>6
mb—>5
mbb—>4
m—>3
mb—>2
m—>1
mbb—>0
mb—>-1

package cn.mb.thread;

import javax.security.auth.PrivateCredentialPermission;

/**

  • 模拟龟兔赛跑
  • @author lenovo

*/
public class Racer implements Runnable {
private static String winner;//胜利者

@Override
public void run() {
for(int steps=1;steps<100;steps++) {
//模拟休息
if(Thread.currentThread().getName().equals(“rabbit”)&&steps%100) {
try {
Thread.sleep(100);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
System.out.println(Thread.currentThread().getName()+"----->"+steps);
boolean flag=gameOver(steps);
if(flag) {
break;
}
}
}
private boolean gameOver(int steps) {
if(winner!=null) {//出现胜利者
return true;
}else {
if(steps
100) {
winner=Thread.currentThread().getName();
System.out.println(“winner---->”+winner);
return true;
}
}
return false;
}
public static void main(String[] args) {
Racer racer=new Racer();
new Thread(racer,“tortoise”).start();
new Thread(racer,“rabbit”).start();
}
}

运行结果:
tortoise----->1
rabbit----->1
rabbit----->2
rabbit----->3
rabbit----->4
rabbit----->5
rabbit----->6
rabbit----->7
rabbit----->8
rabbit----->9
tortoise----->2
tortoise----->3
tortoise----->4
tortoise----->5
tortoise----->6
tortoise----->7
tortoise----->8
tortoise----->9
tortoise----->10
tortoise----->11
tortoise----->12
tortoise----->13
tortoise----->14
tortoise----->15
tortoise----->16
tortoise----->17
tortoise----->18
tortoise----->19
tortoise----->20
tortoise----->21
tortoise----->22
tortoise----->23
tortoise----->24
tortoise----->25
tortoise----->26
tortoise----->27
tortoise----->28
tortoise----->29
tortoise----->30
tortoise----->31
tortoise----->32
tortoise----->33
tortoise----->34
tortoise----->35
tortoise----->36
tortoise----->37
tortoise----->38
tortoise----->39
tortoise----->40
tortoise----->41
tortoise----->42
tortoise----->43
tortoise----->44
tortoise----->45
tortoise----->46
tortoise----->47
tortoise----->48
tortoise----->49
tortoise----->50
tortoise----->51
tortoise----->52
tortoise----->53
tortoise----->54
tortoise----->55
tortoise----->56
tortoise----->57
tortoise----->58
tortoise----->59
tortoise----->60
tortoise----->61
tortoise----->62
tortoise----->63
tortoise----->64
tortoise----->65
tortoise----->66
tortoise----->67
tortoise----->68
tortoise----->69
tortoise----->70
tortoise----->71
tortoise----->72
tortoise----->73
tortoise----->74
tortoise----->75
tortoise----->76
tortoise----->77
tortoise----->78
tortoise----->79
tortoise----->80
tortoise----->81
tortoise----->82
tortoise----->83
tortoise----->84
tortoise----->85
tortoise----->86
tortoise----->87
tortoise----->88
tortoise----->89
tortoise----->90
tortoise----->91
tortoise----->92
tortoise----->93
tortoise----->94
tortoise----->95
tortoise----->96
tortoise----->97
tortoise----->98
tortoise----->99
rabbit----->10
rabbit----->11
rabbit----->12
rabbit----->13
rabbit----->14
rabbit----->15
rabbit----->16
rabbit----->17
rabbit----->18
rabbit----->19
rabbit----->20
rabbit----->21
rabbit----->22
rabbit----->23
rabbit----->24
rabbit----->25
rabbit----->26
rabbit----->27
rabbit----->28
rabbit----->29
rabbit----->30
rabbit----->31
rabbit----->32
rabbit----->33
rabbit----->34
rabbit----->35
rabbit----->36
rabbit----->37
rabbit----->38
rabbit----->39
rabbit----->40
rabbit----->41
rabbit----->42
rabbit----->43
rabbit----->44
rabbit----->45
rabbit----->46
rabbit----->47
rabbit----->48
rabbit----->49
rabbit----->50
rabbit----->51
rabbit----->52
rabbit----->53
rabbit----->54
rabbit----->55
rabbit----->56
rabbit----->57
rabbit----->58
rabbit----->59
rabbit----->60
rabbit----->61
rabbit----->62
rabbit----->63
rabbit----->64
rabbit----->65
rabbit----->66
rabbit----->67
rabbit----->68
rabbit----->69
rabbit----->70
rabbit----->71
rabbit----->72
rabbit----->73
rabbit----->74
rabbit----->75
rabbit----->76
rabbit----->77
rabbit----->78
rabbit----->79
rabbit----->80
rabbit----->81
rabbit----->82
rabbit----->83
rabbit----->84
rabbit----->85
rabbit----->86
rabbit----->87
rabbit----->88
rabbit----->89
rabbit----->90
rabbit----->91
rabbit----->92
rabbit----->93
rabbit----->94
rabbit----->95
rabbit----->96
rabbit----->97
rabbit----->98
rabbit----->99

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值