1. 春运票源紧张,总共只剩下50张长途火车票,车站有6个售票窗口卖票,每秒钟有1张票被售出.
问6个售票窗口一起卖,卖完这50张票要多久时间(时间精确到毫秒)? 请用程序实现.
2. 写一个程序,每秒分三列分别在控制台以"2015-12-01 13:21:01"的时间格式打印出: 日本,中国,英国的当地时间.提示: 
查找API手册,看看这个类的用法: java.util.TimeZone

解决方案 »

  1.   


    import java.util.Date;public class Test{
    public static void main(String[] args){
    Ticket ticket = new Ticket(50);
    Window[] windows = new Window[6];
    long start = new Date().getTime();
    for(int i = 0 ; i < windows.length ; i ++){
    windows[i] = new Window("Window-" + (i + 1),ticket);
    }
    for(int i = 0 ; i < windows.length ; i ++){
    windows[i].start();
    }
    for(int i = 0 ; i < windows.length ; i ++){
    try{
    windows[i].join();
    }catch(InterruptedException e){
    e.printStackTrace();
    return;
    }
    }
    long end = new Date().getTime();
    System.out.printf("%nTotal cost:%.3f seconds.%n",(end - start) / 1000.0);
    }
    }class Window extends Thread{ public Window(String name,Ticket ticket){
    super(name);
    this.ticket = ticket;
    } public void run(){
    while(ticket.hasRemaining()){
    ticket.sellTicket();
    System.out.printf("%s:sell one ticket.%n",Thread.currentThread().getName());
    try{
    Thread.sleep(1000);//1秒钟时间出票
    }catch(InterruptedException e){
    e.printStackTrace();
    return;
    }
    }
    } private Ticket ticket;
    }class Ticket{ public Ticket(int ticketLeft){
    this.ticketLeft = ticketLeft;
    } public synchronized void sellTicket(){
    ticketLeft --;
    } public boolean hasRemaining(){
    return ticketLeft > 0;
    }
    private int ticketLeft;
    }