坦克大战小游戏

//静态代理模式

package cpu;
//
//通过实现Runnable接口来开发线程

public class Thread02 {
    public static void main(String[] args) {
//        Dog dog = new Dog();
        dog.start();
      这里不可以调用start方法
//        Thread t = new Thread(dog);
//        t.start();
        Tiger tiger = new Tiger();
        ThreadProxy threadProxy = new ThreadProxy(tiger);
        threadProxy.start();
    }
}

class  Dog implements Runnable{//线程类

   int count = 0;

    @Override
    public void run() {
        while(true)
        {
            System.out.println("j j j "+(++count)+Thread.currentThread().getName());
            //休息1秒钟
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            if(count == 10)
            {
                break;
            }
        }
    }
}
//线程代理类

class ThreadProxy implements Runnable{
    private  Runnable target = null;// 属性为Runnable类型

    @Override
    public void run() {
        if(target!= null)
        {
            target.run();//动态绑定(运行类型Tiger// )
        }
    }
    public ThreadProxy (Runnable target)
    {
        this.target = target ;
    }

    public void start()
    {
        start0();
//真正实现多线程

    }
    public void start0()
    {
        run();
    }

}
class Animal{}
class Tiger extends Animal implements Runnable{
    @Override
    public void run() {
        System.out.println("嗷嗷叫");
    }
}

你可能感兴趣的