第一题:
package com.pashan; public class Pashan extends Thread{ private int time =0; private int num = 0; public Pashan(int time,String name,int num) { super(name); this.time =time; this.num = num*1000/100; } public void run() { String name = Thread.currentThread().getName(); while(num>0) { try { Thread.sleep(this.time); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println(name+"爬完100米"); num--; } System.out.println(name+"爬到山顶"); } }
package com.pashan; public class PashanDemo { public static void main(String[] args) { Pashan xz = new Pashan(500,"欣仔",1); Pashan xd = new Pashan(500,"小邓",1); Pashan db = new Pashan(500,"刀疤",1); Pashan jj = new Pashan(500,"静静",1); xz.start(); xd.start(); db.start(); jj.start(); } }
显示结果:
第二题:
package com.kanbing; public class VipRunnable implements Runnable{ @Override public void run() { for(int i =1;i<=20;i++) { try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println(Thread.currentThread().getName()+":"+i+"在看病"); } } }
package com.kanbing; public class CommonDemo { public static void main(String[] args) { VipRunnable b = new VipRunnable(); Thread b2 = new Thread(b,"VIP普通病人"); b2.start(); for(int i =1;i<=50;i++) { Thread.currentThread().setName("普通病人"); if(i==10) { try { b2.join(); } catch (InterruptedException e) { e.printStackTrace(); } } try { Thread.sleep(500); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println(Thread.currentThread().getName()+":"+i+"在看病"); } } }
输出结果:
第三题:
package com.run; public class RelayRace implements Runnable{ @Override public void run() { running(); } public synchronized void running() { System.out.println(Thread.currentThread().getName()+":接过接力棒"); for(int i =1;i<=10;i++) { try { Thread.sleep(500); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println(Thread.currentThread().getName()+":"+"跑了"+(i*10)+"米"); } } }
package com.run; public class RelayRaceDemo { public static void main(String[] args) { RelayRace rr = new RelayRace(); Thread r1 =new Thread(rr,"1号"); Thread r2 =new Thread(rr,"2号"); Thread r3 =new Thread(rr,"3号"); Thread r4 =new Thread(rr,"4号"); r1.start(); r2.start(); r3.start(); r4.start(); } }
输出结果为:
第四题:
package com.piao;
public class Piao implements Runnable{
private int num;
private int count = 20;
@Override
public void run() {
while(true) {
try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
synchronized (this) {
if(count<=0) {
break;
}
num++;
count--;
System.out.println(Thread.currentThread().getName()+"抢到了第"+num+"张票,还剩"+count+"张票");
if(Thread.currentThread().getName().equals("黄牛党")) {
return;
}
}
}
}
}
package com.piao; public class PiaoDemo { public static void main(String[] args) { Piao p = new Piao(); Thread p1 = new Thread(p,"逃跑跑"); Thread p2 = new Thread(p,"张票票"); Thread p3 = new Thread(p,"黄牛党"); p1.start(); p2.start(); p3.start(); } }
输出结果为:
百度网盘链接:https://pan.baidu.com/s/1sxJkUUyPmVzSK0k9AL1_Kg
提取码:n3ne
复制这段内容后打开百度网盘手机App,操作更方便哦