黑马程序员技术交流社区

标题: 请用Java相关知识写出下列的代码 [打印本页]

作者: abckids    时间: 2016-7-8 23:31
标题: 请用Java相关知识写出下列的代码
模拟两个人向银行存钱,每次存300,两个人总共存十次

作者: itshenma    时间: 2016-7-8 23:31
package com.itheima.day19;

public class Test01 {
        public static void main(String[] args) {
                /*
                 * 模拟两个人向银行存钱,每次存300,两个人总共存十次
                 */
                MyRunnable mr = new MyRunnable();
                new Thread(mr,"MCC").start();
                new Thread(mr,"BZ").start();
        }
}


class MyRunnable implements Runnable {
        private int count = 1;
        @Override
        public void run() {
                while(true) {
                        try {
                                Thread.sleep(20);
                        } catch (InterruptedException e) {
                                e.printStackTrace();
                        }
                        synchronized (this) {
                                if (count <= 10) {
                                        //存钱
                                        System.out.println("第" + count++ + "次,存300块钱的人是:" + Thread.currentThread().getName());
                                }else {
                                        return;
                                }
                        }
                }
        }
       
}
作者: LoveGG    时间: 2016-7-9 00:59
楼上的非常好.给赞,上课的实例,
作者: Leslie_YgOXQ    时间: 2016-7-19 00:16
66666666666666




欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) 黑马程序员IT技术论坛 X3.2