zoukankan      html  css  js  c++  java
  • JAVA入门到精通-第39讲-线程.坦克大战7

     如果没有做要求,用实现接口的方法写进程;
     至少有继承的机会;
     
     实际上,更多的情况下是多线程计算;
     
     两个线程,t1/t2,同时启动;
     创建了一只猪,创建了一只鸟;
     第一个线程承载猪,第二个线程承载鸟;
     很可能是交替进行,但也未必;和休眠时间相关;
     
     
     同时去争夺一种资源,线程同步的问题;
     
     
     Thread类本身就实现了Runnable接口,本身是一家人;
     启动的方式不太一样;
     
     坦克的子弹自动跑(肯定用到线程):
     子弹也是一个对象:有坐标,有速度;
     可以当做线程的一个类,可以自动跑动;
     
     子弹也是属于某个坦克的;
      
     
     
     
     
     这个坦克永远只能有一颗子弹;

     当用户按下J的时候,子弹发射:KeyPressed
      else if 的致命毛病是找到一个入口,
      不会去判断另外的入口;
       按了W键,子弹就可能出不来了;
     
     
     绘制子弹:
     其实是一个点:
     
     
    怎么让子弹跑起来?
    让子弹的坐标不停的得到修改。
     把子弹做成一个线程,不停地修改坐标达到目的;
    传方向进去:
     
     传进速度:
     
     
     //子弹何时死亡?

    启动子弹:
     虽然子弹坐标在变化,但子弹不变;
    paint函数需要不停地重绘,子弹才能跑;
    把MyPanel也做成一个线程;
     屏幕本身也在不停的刷新:
     
     
    还得启动MyPanel线程
     
    但是,打一颗子弹,这子弹永远在跑:
    当子弹碰到敌人坦克/边框的时候子弹死亡;
    boolean isLive=true;
    ----------------------------------------------------------
     多线程实例演示[Thread03.java]
    x
     
    1
    /**
    2
     *多线程Thread使用
    3
     *1、一个线程通过接收n来执行1+..+n得到和
    4
     *2、另一线程每隔1秒输出一次hello world!
    5
     */
    6
    public class Thread03 {
    7
        public static void main(String[] args) {
    8
            Pig pig=new Pig(10);
    9
            Bird bird=new Bird(10);
    10
            //建立线程
    11
            Thread t1=new Thread(pig);
    12
            Thread t2=new Thread(bird);
    13
            //启动线程
    14
            t1.start();
    15
            t2.start();
    16
        }
    17
    }
    18
    19
    //打印hello world!
    20
    class Pig implements Runnable{
    21
        int n=0;
    22
        int times=0;
    23
        public Pig(int n){
    24
            this.n=n;
    25
        }
    26
        
    27
        public void run(){
    28
            while(true){
    29
                try {
    30
                    Thread.sleep(1000);
    31
                } catch (Exception e) {
    32
                    e.printStackTrace();
    33
                }
    34
                times++;
    35
                System.out.println("我是一个线程,正在输出第"+times+"个hello world!");
    36
                if(times==n){
    37
                    break;
    38
                }
    39
            }
    40
        }
    41
    }
    42
    43
    //算数学题
    44
    class Bird implements Runnable{//多线程时应使用implements接口来实现,不要使用extends继承
    45
        int n=0;
    46
        int res=0;
    47
        int times=0;
    48
        public Bird(int n){
    49
            this.n=n;
    50
        }
    51
        
    52
        public void run() {
    53
            while(true){
    54
                try {
    55
                    Thread.sleep(1000);
    56
                } catch (Exception e) {
    57
                    e.printStackTrace();
    58
                }
    59
                res+=(++times);
    60
                System.out.println("当前结果是:"+res);
    61
                if(times==n){
    62
                    System.out.println("最后的结果是:"+res);
    63
                    break;
    64
                }
    65
            }
    66
        }
    67
    }
    68
     
    MyTank03:

     




  • 相关阅读:
    1038 Recover the Smallest Number (30分) sort-cmp妙用(用于使字符串序列最小)
    1033 To Fill or Not to Fill (25分)贪心(???)
    1030 Travel Plan (30分) dij模板题
    1020 Tree Traversals (25分)(树的构造:后序+中序->层序)
    1022 Digital Library (30分) hash模拟
    1018 Public Bike Management (30分)(Dijkstra路径保存fa[]+DFS路径搜索)
    1017 Queueing at Bank (25分)模拟:关于事务排队处理
    1014 Waiting in Line (30分)队列模拟题
    1010 Radix (25分)暴力猜数or二分猜数
    HDU 3032 multi-sg 打表找规律
  • 原文地址:https://www.cnblogs.com/xuxaut-558/p/10045744.html
Copyright © 2011-2022 走看看