task1 2 - sarasayhi/hello-world GitHub Wiki

package exam;

import java.util.HashMap; import java.util.Map;

public class max {

public static void main(String[] args) {
	String[] list = { "wo", "ni", "ta", "wo", "ni", "ta", "wo", "ni", "ta",
			"wo", "ni", "ta", "wo", "ni", "ta","ta","ta","ta","ta","ta" };
	Map<String, Integer> result = new HashMap<String, Integer>();
	Map<String, Integer> resMap = find(result, list, 0, 1);
	String max = null;
	for (String s : resMap.keySet()) {
		if(max == null){
			max = s;
		}else {
			if(resMap.get(max) < resMap.get(s)){
				max = s;
			}
		}
	}
	System.out.println(max);
	System.out.println(resMap.get(max));
}

public static Map<String, Integer> find(Map<String, Integer> result,
		String[] list, int cur, int compare) {
	int init = cur;
	while (compare <= list.length - 1) {
		if (list[compare].equals(list[cur])) {
			String tmp = list[compare];
			cur++;
			list[compare] = list[cur];
			list[cur] = tmp;
		}
		compare++;
	}
	result.put(list[cur], cur - init);

	if (cur == list.length - 1) {
		return result;
	} else {
		compare = cur + 2;
		cur = cur + 1;
		return find(result, list, cur, compare);
	}
}

}

package exam;

import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors;

public class ThreadPoolExecutorTest {

static int ticket = 1;


public static void main(String[] args) throws InterruptedException {
	ExecutorService fixedThreadPool = Executors.newFixedThreadPool(4);
	while (ticket < 101) {
	fixedThreadPool.execute(new Runnable() {
		public void run() {
			synchronized (Ticket.class) {
				try {
						System.out.println(Thread.currentThread().getName()
								+ " :卖出 " + ticket + "号票");
						ticket++;
					
					Thread.sleep(1000);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
			}
		}
		
	});
	}
	
	fixedThreadPool.shutdown();  
    while (true) {  
        if (fixedThreadPool.isTerminated()) {  
            System.out.println("结束了!");  
            break;  
        }  
        Thread.sleep(200);  
    }  

}

}