1、模拟多人爬山
package com.work.climb; public class ClimbRunnable extends Thread{ private int time; private int num; public ClimbRunnable() {} public ClimbRunnable(int time, String name,int num) { super(name); this.time = time; this.num = num; } public void run() { while(this.num>0) { try { Thread.sleep(this.time); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println(Thread.currentThread().getName()+"已爬100米"); this.num = this.num - 1; } System.out.println(Thread.currentThread().getName()+"爬到山顶!"); } public static void main(String[] args) { ClimbRunnable c1 = new ClimbRunnable(200,"年轻人",10); ClimbRunnable c2 = new ClimbRunnable(500,"老年人",10); c1.start(); c2.start(); } }
结果示例:
2、模拟叫号看病
package com.java.doctor; public class DoctorThread extends Thread{ private int number;//看病人数 public DoctorThread() { super(); } public DoctorThread(String name,int number) { super(name); this.number = number; } public int getNumber() { return number; } public void setNumber(int number) { this.number = number; } public void run() { for(int i = 1;i<=this.number;i++) { System.out.println(Thread.currentThread().getName()+":"+i+"号病人正在看病!"); } } public static void main(String[] args) { DoctorThread special = new DoctorThread("特需号",10); //将主进程重命名为普通号 Thread.currentThread().setName("普通号"); // special.setPriority(10); // ordinary.setPriority(1); special.start(); try { for(int i = 1;i<=50;i++) { //如果普通号到49号后,特需号还未看完,先暂停普通号,看完特需号再继续 if(i == 49) { special.join(); } System.out.println(Thread.currentThread().getName()+":"+i+"号病人正在看病!"); } } catch (InterruptedException e) { e.printStackTrace(); } } }
结果示例:
3、模拟接力赛跑
package com.java.race; /** * 模拟接力赛跑 * @author Administrator * */ public class Race implements Runnable{ private int length = 1000; //剩余赛程 public void run() { String name = Thread.currentThread().getName(); while(true) { synchronized(this) { if(length<=0) { break; } length -= 100; try { Thread.sleep(200); } catch (InterruptedException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } System.out.println(name+"接力!"); for(int i = 1;i<11;i++) { System.out.println(name+"跑了"+i*10+"米"); } } try { Thread.sleep(500); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } public static void main(String[] args) { Race r = new Race(); Thread t1 = new Thread(r,"1号选手"); Thread t2 = new Thread(r,"2号选手"); Thread t3 = new Thread(r,"3号选手"); Thread t4 = new Thread(r,"4号选手"); t1.start(); t2.start(); t3.start(); t4.start(); } }
结果示例:
4、模拟网站抢票
package com.java.ticket; /** * 模拟网站抢票系统,有三个用户:桃跑跑、黄牛党、张票票,如果是黄牛党只允许抢购一张 * @author Administrator * */ public class Ticket implements Runnable{ private int count = 10; //剩余票数 private int num = 0; //当前用户抢到的第几张票 public void run() { String name = Thread.currentThread().getName(); while(true) { //根据sale()方法返回值判断是否为黄牛党,如果是结束线程 if(sale(name)) { break; } } } public synchronized boolean sale(String name) { if(count<=0) { return true; } count--; num++; try { Thread.sleep(100); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println(name+"抢到第"+num+"张票,"+"剩余"+count+"张票。"); //判断线程名是否为黄牛党,如果是返回true if(name.equals("黄牛党")) { return true; } return false; } public static void main(String[] args) { Ticket t = new Ticket(); Thread t1 = new Thread(t,"桃跑跑"); Thread t2 = new Thread(t,"黄牛党"); Thread t3 = new Thread(t,"张票票"); t1.start(); t2.start(); t3.start(); } }
结果示例: