Java编程思想21.2.1实例

package t21_2_1;

 

public class Liftoff implements Runnable {

    protected int countDown = 100;

    private static int taskCount = 0;

    private final int id = taskCount++;

 

    public Liftoff() {

    }

 

    public Liftoff(int countDown) {

        this.countDown = countDown;

    }

 

    public String status() {

        return "#" + id + "(" + (countDown > 0 ? countDown : "Liftoff!") + "),";

    }

 

    public static void main(String[] args) {

        // TODO Auto-generated method stub

        /*

         * Liftoff launtch = new Liftoff(); launtch.run();

         */

        for (int i = 0; i < 5; i++) {

            new Thread(new Liftoff()).start();

            System.out.println("wait for you!");

        }

 

    }

 

    @Override

    public void run() {

        // TODO Auto-generated method stub

        while (countDown-- > 0) {

            System.out.println(status());

            Thread.yield();

 

        }

 

    }

}

 

 

运行结果:

wait for you!

wait for you!

wait for you!

wait for you!

#0(99),

wait for you!

#2(99),

#1(99),

#3(99),

#0(98),

#2(98),

#3(98),

#1(98),

#0(97),

#2(97),

#3(97),

#4(99),

#3(96),

#2(96),

#0(96),

#1(97),

#4(98),

#3(95),

#2(95),

#0(95),

#1(96),

#4(97),

#3(94),

#2(94),

#0(94),

#2(93),

#2(92),

#2(91),

#2(90),

#2(89),

#2(88),

#2(87),

#2(86),

#2(85),

#2(84),

#2(83),

#2(82),

#2(81),

#4(96),

#3(93),

#3(92),

#3(91),

#1(95),

#1(94),

#3(90),

#4(95),

#4(94),

#4(93),

#4(92),

#4(91),

#4(90),

#2(80),

#2(79),

#0(93),

#2(78),

#2(77),

#2(76),

#2(75),

#4(89),

#3(89),

#3(88),

#3(87),

#3(86),

#3(85),

#1(93),

#3(84),

#3(83),

#3(82),

#4(88),

#2(74),

#2(73),

#0(92),

#4(87),

#3(81),

#1(92),

#2(72),

#0(91),

#3(80),

#4(86),

#1(91),

#2(71),

#1(90),

#2(70),

#2(69),

#2(68),

#2(67),

#2(66),

#2(65),

#2(64),

#2(63),

#2(62),

#2(61),

#2(60),

#2(59),

#2(58),

#2(57),

#2(56),

#2(55),

#4(85),

#3(79),

#3(78),

#3(77),

#3(76),

#3(75),

#0(90),

#3(74),

#4(84),

#2(54),

#2(53),

#2(52),

#2(51),

#2(50),

#1(89),

#2(49),

#4(83),

#3(73),

#0(89),

#3(72),

#4(82),

#2(48),

#1(88),

#2(47),

#4(81),

#3(71),

#0(88),

#3(70),

#4(80),

#2(46),

#1(87),

#2(45),

#4(79),

#4(78),

#4(77),

#4(76),

#3(69),

#0(87),

#3(68),

#4(75),

#2(44),

#1(86),

#2(43),

#4(74),

#3(67),

#0(86),

#3(66),

#4(73),

#2(42),

#1(85),

#2(41),

#4(72),

#3(65),

#0(85),

#3(64),

#4(71),

#2(40),

#1(84),

#2(39),

#4(70),

#3(63),

#0(84),

#3(62),

#4(69),

#2(38),

#2(37),

#2(36),

#2(35),

#2(34),

#2(33),

#2(32),

#2(31),

#2(30),

#2(29),

#1(83),

#2(28),

#2(27),

#2(26),

#2(25),

#2(24),

#2(23),

#2(22),

#2(21),

#2(20),

#2(19),

#2(18),

#2(17),

#2(16),

#2(15),

#4(68),

#3(61),

#0(83),

#3(60),

#4(67),

#2(14),

#1(82),

#2(13),

#4(66),

#3(59),

#0(82),

#3(58),

#4(65),

#2(12),

#1(81),

#2(11),

#4(64),

#3(57),

#0(81),

#3(56),

#4(63),

#2(10),

#1(80),

#2(9),

#4(62),

#3(55),

#0(80),

#3(54),

#4(61),

#2(8),

#1(79),

#2(7),

#4(60),

#3(53),

#4(59),

#0(79),

#2(6),

#1(78),

#3(52),

#2(5),

#0(78),

#4(58),

#1(77),

#2(4),

#0(77),

#1(76),

#2(3),

#3(51),

#4(57),

#3(50),

#2(2),

#1(75),

#2(1),

#0(76),

#3(49),

#4(56),

#2(Liftoff!),

#1(74),

#0(75),

#3(48),

#4(55),

#1(73),

#0(74),

#3(47),

#0(73),

#1(72),

#4(54),

#1(71),

#0(72),

#3(46),

#0(71),

#1(70),

#4(53),

#1(69),

#0(70),

#3(45),

#0(69),

#1(68),

#4(52),

#1(67),

#0(68),

#3(44),

#0(67),

#1(66),

#4(51),

#1(65),

#0(66),

#3(43),

#0(65),

#1(64),

#4(50),

#1(63),

#0(64),

#3(42),

#0(63),

#1(62),

#4(49),

#1(61),

#0(62),

#3(41),

#0(61),

#1(60),

#4(48),

#1(59),

#0(60),

#3(40),

#0(59),

#1(58),

#4(47),

#1(57),

#0(58),

#3(39),

#0(57),

#1(56),

#4(46),

#1(55),

#0(56),

#3(38),

#0(55),

#1(54),

#4(45),

#1(53),

#0(54),

#3(37),

#0(53),

#1(52),

#4(44),

#1(51),

#0(52),

#3(36),

#0(51),

#0(50),

#0(49),

#0(48),

#0(47),

#1(50),

#4(43),

#4(42),

#4(41),

#4(40),

#4(39),

#4(38),

#4(37),

#1(49),

#0(46),

#0(45),

#0(44),

#0(43),

#0(42),

#0(41),

#0(40),

#0(39),

#0(38),

#0(37),

#0(36),

#0(35),

#0(34),

#0(33),

#3(35),

#0(32),

#1(48),

#4(36),

#4(35),

#4(34),

#4(33),

#4(32),

#4(31),

#1(47),

#0(31),

#3(34),

#0(30),

#1(46),

#4(30),

#1(45),

#0(29),

#3(33),

#0(28),

#1(44),

#4(29),

#1(43),

#0(27),

#3(32),

#0(26),

#1(42),

#4(28),

#1(41),

#0(25),

#3(31),

#0(24),

#1(40),

#4(27),

#1(39),

#0(23),

#3(30),

#4(26),

#1(38),

#1(37),

#1(36),

#1(35),

#1(34),

#1(33),

#1(32),

#1(31),

#1(30),

#1(29),

#1(28),

#1(27),

#1(26),

#1(25),

#1(24),

#1(23),

#1(22),

#1(21),

#1(20),

#1(19),

#1(18),

#1(17),

#1(16),

#1(15),

#1(14),

#1(13),

#1(12),

#1(11),

#1(10),

#1(9),

#1(8),

#1(7),

#1(6),

#1(5),

#1(4),

#1(3),

#1(2),

#1(1),

#1(Liftoff!),

#3(29),

#0(22),

#3(28),

#3(27),

#3(26),

#3(25),

#3(24),

#3(23),

#3(22),

#3(21),

#3(20),

#3(19),

#3(18),

#3(17),

#3(16),

#3(15),

#3(14),

#3(13),

#3(12),

#3(11),

#4(25),

#0(21),

#4(24),

#4(23),

#3(10),

#3(9),

#3(8),

#3(7),

#3(6),

#3(5),

#3(4),

#3(3),

#3(2),

#3(1),

#3(Liftoff!),

#4(22),

#0(20),

#0(19),

#4(21),

#0(18),

#4(20),

#0(17),

#4(19),

#0(16),

#4(18),

#0(15),

#4(17),

#0(14),

#4(16),

#0(13),

#4(15),

#0(12),

#4(14),

#0(11),

#4(13),

#0(10),

#4(12),

#0(9),

#4(11),

#0(8),

#4(10),

#0(7),

#4(9),

#0(6),

#4(8),

#0(5),

#4(7),

#0(4),

#4(6),

#0(3),

#4(5),

#0(2),

#4(4),

#0(1),

#4(3),

#0(Liftoff!),

#4(2),

#4(1),

#4(Liftoff!),

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值