练习三
package zuoYe02; public class Person implements Runnable { private int time; // 自己定义有十个一百米 每跑一百米,则减一 为0时则到达终点 public int count = 10; public int getTime() { return time; } public void setTime(int time) { this.time = time; } public Person(int time) { super(); this.time = time; } @Override public void run() { // 程序开始运行 // boolean boo = false; while (true) { if (count == 0) { System.out.println(Thread.currentThread().getName() + "到达终点"); // boo = true; return; } --count; System.out.println(Thread.currentThread().getName() + "爬完100米!"); try { Thread.sleep(this.time); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } }
package zuoYe02; public class TestPersonThread { public static void main(String[] args) { Person y = new Person(500); Person o = new Person(900); Thread t1 = new Thread(y,"年轻人"); Thread t2 = new Thread(o,"老年人"); t1.start(); t2.start(); } }
本文作者:AxeBurner
本文链接:https://www.cnblogs.com/bichen-01/p/11308894.html
版权声明:本作品采用知识共享署名-非商业性使用-禁止演绎 2.5 中国大陆许可协议进行许可。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步