java程序设计基础_陈国君版第五版_第十一章例题
2016-03-13 20:01
381 查看
java程序设计基础_陈国君版第五版_第十一章习题
class MyThread extends Thread { private String who; public MyThread(String who) { this.who = who; } public void run() { for(int i =0 ; i<5; i++) { try { sleep((int)(1000*Math.random())); } catch(InterruptedException e) {} System.out.println(who+"正在运行!!"); } } } public class Main11_1 { public static void main(String[] args){ MyThread you = new MyThread("你"); MyThread she = new MyThread("她"); you.start(); she.start(); System.out.println("主方法main()运行结束!"); } }
class MyThread2 implements Runnable { private String who; public MyThread2(String who) { this.who = who; } public void run() { for(int i =0 ;i<5;i++) { try { Thread.sleep((int)(1000*Math.random())); } catch(InterruptedException e) { System.out.println(e.toString()); } System.out.println(who+"正在运行!!"); } } } public class Main11_2 { public static void main(String[] args) { MyThread you = new MyThread("你"); MyThread she = new MyThread("她"); Thread t1 = new Thread(you); Thread t2 = new Thread(she); t1.start(); t2.start(); } }
class MyThread3 extends Thread { private String who; public MyThread3(String who) { this.who = who; } public void run() { for(int i =0 ; i<5; i++) { try { sleep((int)(1000*Math.random())); } catch(InterruptedException e) {} System.out.println(who+"正在运行!!"); } } } public class Main11_3 { public static void main(String[] args) { MyThread3 you = new MyThread3("你"); MyThread3 she = new MyThread3("她"); you.start(); try { you.join(); } catch (InterruptedException e) { e.printStackTrace(); } she.start(); try { she.join(); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println("主方法main()运行结束!!"); } }
class ThreadSale extends Thread { private int tickets = 10; public void run() { while(true) { if(tickets>0) System.out.println(getName()+" 售机票第"+tickets--+"号"); else System.exit(0); } } } public class Main11_4 { public static void main(String[] args) { ThreadSale t1 = new ThreadSale(); ThreadSale t2 = new ThreadSale(); ThreadSale t3 = new ThreadSale(); t1.start(); t2.start(); t3.start(); } }
class ThreadSale5 implements Runnable { private int tickets = 10; public void run() { while(true) { if(tickets>0) System.out.println(Thread.currentThread().getName()+"售机票第"+tickets--+"号"); else System.exit(0); } } } public class Main11_5 { public static void main(String[] args){ ThreadSale5 t = new ThreadSale5(); Thread t1 = new Thread(t,"第1售票窗口"); Thread t2 = new Thread(t,"第2售票窗口"); Thread t3 = new Thread(t,"第3售票窗口"); t1.start(); t2.start(); t3.start(); } }
class Mbank { private static int sum = 2000; public static void take(int k) { int temp = sum; temp -= k; try { Thread.sleep((int)(1000*Math.random())); } catch(InterruptedException e) {} sum =temp; System.out.println("sum ="+sum); } } class Customer extends Thread { public void run() { for(int i = 1;i<=4;i++) Mbank.take(100); } } public class Main11_6 { public static void main(String[] args){ Customer c1 = new Customer(); Customer c2 = new Customer(); c1.start(); c2.start(); } }
class Mbank7 { private static int sum = 2000; public synchronized static void take(int k) { int temp = sum; temp -= k; try { Thread.sleep((int)(1000*Math.random())); } catch(InterruptedException e) {} sum =temp; System.out.println("sum ="+sum); } } class Customer7 extends Thread { public void run() { for(int i = 1;i<=4;i++) Mbank7.take(100); } } public class Main11_7 { public static void main(String[] args){ Customer7 c1 = new Customer7(); Customer7 c2 = new Customer7(); c1.start(); c2.start(); } }
public class Main11_8 { public static void main(String[] args){ Tickets t = new Tickets(10); new Producer(t).start(); new Consumer(t).start(); } } class Tickets { protected int size; int number = 0 ; boolean available = false; public Tickets(int size) { this.size = size; } public synchronized void put() { if(available) try{wait();} catch(Exception e){} System.out.println("存入第【"+(++number)+"】号票"); available = true; notify(); } public synchronized void sell() { if(!available) try{wait();} catch(Exception e){} System.out.println("售出第【"+(number)+"】号票"); available = false; notify(); if(number == size) number =size+1; } } class Producer extends Thread { Tickets t = null; public Producer(Tickets t) { this.t = t; } public void run() { while(t.number < t.size) t.put(); } } class Consumer extends Thread { Tickets t = null; public Consumer(Tickets t) { this.t = t; } public void run() { while(t.number <= t.size) t.sell(); } }
相关文章推荐
- java对世界各个时区(TimeZone)的通用转换处理方法(转载)
- java-注解annotation
- java-模拟tomcat服务器
- java-用HttpURLConnection发送Http请求.
- java-WEB中的监听器Lisener
- Android IPC进程间通讯机制
- Android Native 绘图方法
- Android java 与 javascript互访(相互调用)的方法例子
- 介绍一款信息管理系统的开源框架---jeecg
- 聚类算法之kmeans算法java版本
- java实现 PageRank算法
- PropertyChangeListener简单理解
- c++11 + SDL2 + ffmpeg +OpenAL + java = Android播放器
- 插入排序
- 冒泡排序
- 堆排序
- 快速排序