【作业】3月9日

第一题:

 1 package com.Thread;
 2 /**
 3  * 模拟爬山
 4  * @author 5  *
 6  */
 7 public class ThreadDome extends Thread {
 8     private int    shij;
 9     private  int  mishu;
10     public ThreadDome( int shij, int mishu) {
11         super();
12         this.shij = shij;
13         this.mishu = mishu*100;
14     }
15     
16     public void run() {
17         for(int i=0;i<10;i++) {
18             try {
19                 Thread.sleep(this.shij);
20             } catch (InterruptedException e) {
21                 e.printStackTrace();
22             }
23             System.out.println(Thread.currentThread().getName()+"爬了"+mishu*(i+1)+"米");
24         }
25         System.out.println(Thread.currentThread().getName()+"到达终点");
26     }
27 }

 1 package com.Thread;
 2 
 3 public class ThreadDomeTest {
 4 
 5     public static void main(String[] args) {
 6         ThreadDome td = new ThreadDome(500,1);
 7         ThreadDome td2 = new ThreadDome(1500,1);
 8         Thread t1 = new Thread(td,"年轻人------");
 9         Thread t2 = new Thread(td2,"老年人>>>>>");
10         t1.start();
11         t2.start();
12     }
13 
14 }

输出结果图:

第二题

 1 package com.Thread;
 2 /**
 3  * 创建线程
 4  * 
 5  *
 6  */
 7 public class RunnableDome implements Runnable{
 8     //子线程
 9     public void run() {
10         for(int i = 1;i<=20;i++) {
11             try {
12                 Thread.sleep(2000);
13             } catch (InterruptedException e) {
14                 e.printStackTrace();
15             }
16             System.out.println(Thread.currentThread().getName()+":"+i+"在看病");
17         }
18     }
19 }
 1 package com.Thread;
 2 
 3 public class RunnableTest {
 4 
 5     public static void main(String[] args) {               
 6         RunnableDome t = new RunnableDome();
 7         Thread t2 = new Thread(t,"VIP病人");
 8         t2.start();
 9         for(int i=1;i<=50;i++) {
10             Thread.currentThread().setName("普通病人");
11             if(i==10) {
12                 try {
13                     t2.join();
14                 } catch (InterruptedException e) {
15                     e.printStackTrace();
16                 }
17             }
18             try {
19                 Thread.sleep(1000);
20             } catch (InterruptedException e) {
21                 e.printStackTrace();
22             }
23             System.out.println(Thread.currentThread().getName()+":"+i+"在看病");
24         }
25     }
26 
27 }

输出结果图:

第三题:

 1 package com.Thread;
 2 
 3 public class maipiao implements Runnable {
 4 
 5     public void run() {
 6             paobu();
 7     }
 8     
 9     public synchronized void paobu() {
10         System.out.println(Thread.currentThread().getName()+":接过接力棒");
11         for(int j=1;j<=10;j++) {
12             try {
13                 Thread.sleep(500);
14             } catch (InterruptedException e) {
15                 e.printStackTrace();
16             }
17             System.out.println(Thread.currentThread().getName()+":"+"跑了"+j*10+"米");
18         }
19 
20     }
21     public static void main(String[] args) {
22         maipiao mp = new maipiao();
23         Thread t1 = new Thread(mp,"1号");
24         Thread t2 = new Thread(mp,"2号");
25         Thread t3 = new Thread(mp,"3号");
26         Thread t4 = new Thread(mp,"4号");
27         t1.start();
28         t2.start();
29         t3.start();
30         t4.start();
31     
32     }
33 }

输出结果图:

第四题:

 1 package com.Thread;
 2 
 3 public class qiaoDome implements Runnable {
 4     private  int piao = 20;   //票数
 5     private  int jipiao = 0;  //记录票数的
 6 
 7 
 8     @Override
 9     public void run() {
10         String s = Thread.currentThread().getName();
11         while(true) {
12             synchronized (this) {
13                 if(piao<=0) {
14                     break;
15                 }
16                 piao--;
17                 jipiao++;    
18                 System.out.println(s+"抢了第"+jipiao+"票");
19             }
20             if(s.equals("黄牛党")) {
21                 break;
22             }
23         }
24     }
25 
26 
27 }
 1 package com.Thread;
 2 
 3 public class qiaoTest {
 4 
 5     public static void main(String[] args) {
 6         qiaoDome  qd = new qiaoDome();
 7         Thread t1 = new Thread(qd,"逃跑跑");
 8         Thread t2 = new Thread(qd,"张飘飘");
 9         Thread t3 = new Thread(qd,"黄牛党");
10         t1.start();
11         t2.start();
12         t3.start();
13     }
14 
15 }

输出结果图:

 

posted @ 2019-03-09 16:39  XiaoZheJun  阅读(147)  评论(0编辑  收藏  举报