//静态代理模式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();
}
}classDog 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{
privateRunnable 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("嗷嗷叫");
}
}
【坦克大战|坦克大战小游戏】
推荐阅读
- bug|太极限了,JDK的这个BUG都能被我踩到
- SpringBoot|几行代码实现SpringBoot图片上传
- Java|不重启服务进行功能开发
- java|Springboot 一文搞懂AOP面向切面编程
- maven|Maven---彻底理解聚合项目和父子模块的关系
- Spring|Lombok 妙用之@RequiredArgsConstructo注解
- Linux|一文带你学会Shell基操
- JavaEE|【Spring Boot】SpringBoot 配置文件详解
- springboot|springboot配置文件详解