两道面试题解答之一 Trading Server Deadlocks

https://bridgewater.interviewstreet.com/challenges/

第一题是关于判断死锁问题的

Trading Server Deadlocks

A few months ago, we were running a new release of our trade server in QA and experienced a hang. The system was accepting connections from clients, but no longer processing any transactions. This system is critical to many of our major trading operations and so, the issue was a big deal to fix before we released to production.

The trade server supports many different execution paths which take out various locks on shared resources.  Upon investigation, we found that the system had experienced a deadlock between two of those execution paths. To ensure we fixed the problem holistically (rather than just the deadlock case that we caught), we wanted to examine all the execution paths in the system and determine all cases that were constructed in a way that they can cause deadlocks.

We'll provide you a set of execution paths. Each is written as an ordered list of lock acquisitions. Your job is to write a program which enumerate all the subsets of sets of execution paths that could result in a deadlock if executed in parallel.

Your input will be a file with one execution path per line. The name will be first, separated by a space, then a comma separated list of the locks in order they are acquired. For instance:

    path1 L1,L2,L3
    path2 L2,L1,L3
    path3 L4,L5,L6
    path4 L5,L4,L6

Your output should be each subset of execution paths which can deadlock, one per line. Sort the paths alphabelitcaly and order the sets alphabetically based on their lexicographic representation.

    path1,path2
    path3,path4

For more information about deadlocks, checkout the wikipedia page.


package bridgewater;

import java.io.*;
import java.util.*;
import java.util.Map.Entry;

public class DeadLockSolution {
	
	private List<String> inputs = new ArrayList<String>();
	
	public DeadLockSolution(){
	}
	
	public static void main(String[] args) throws Exception {
		BufferedReader br = null;
		DeadLockSolution solution = new DeadLockSolution();
		try {
			InputStream inputStream = new FileInputStream("input.txt");
			Reader reader = new InputStreamReader(inputStream);
			br = new BufferedReader(reader);
			
//			br = new BufferedReader(new InputStreamReader(System.in));
			
			String input = br.readLine();
			while(input != null){
				solution.inputs.add(input);
				input = br.readLine();
			}
			solution.process(solution.inputs);
			
		} finally {
			if (br != null)
				br.close();
		}
	}

	public void process(List<String> inputs) {
		LinkedHashMap<String, ArrayList<String>> map = new LinkedHashMap<String, ArrayList<String>>();
		
		for (String string : inputs) {
			String[] splits = string.split(" ");
			if(splits.length > 1){
				String[] tmp = splits[1].split(",");
				ArrayList<String> locks = new ArrayList<String>();
				locks.addAll(Arrays.asList(tmp));
				map.put(splits[0], locks);
			}
		}
//		print(map);
		
		LinkedHashSet<ArrayList<String>> ret = new LinkedHashSet<ArrayList<String>>();
		int entryIndex = 0;
		for (Entry<String, ArrayList<String>> entry : map.entrySet()) {
			ArrayList<String> value = entry.getValue();
			int i = 0;
			do{
				int otherEntryIndex = 0;
				for(Entry<String, ArrayList<String>> otherEntry : map.entrySet()){
					if(otherEntryIndex>entryIndex && !entry.equals(otherEntry)){
						ArrayList<String> otherValue = otherEntry.getValue();
						if(otherValue.contains(value.get(i))){
							int FirstHitIndex = otherValue.indexOf(value.get(i));
							if((i+1)%value.size() == 0){		// Special case, when looping around!
								if(FirstHitIndex+1<otherValue.size() && otherValue.get(FirstHitIndex+1).equals(value.get((i+1)%value.size()))){
									ArrayList<String> conflicts = new ArrayList<String>();
									conflicts.add(entry.getKey());
									conflicts.add(otherEntry.getKey());
									ret.add(conflicts);
								}
							}else{
								if(FirstHitIndex-1>=0 && otherValue.get(FirstHitIndex-1).equals(value.get((i+1)%value.size()))){
									ArrayList<String> conflicts = new ArrayList<String>();
									conflicts.add(entry.getKey());
									conflicts.add(otherEntry.getKey());
									ret.add(conflicts);
								}
							}
						}
					}
					otherEntryIndex++;
				}
				i++;
				i %= value.size();
			}while(i != 0);
			entryIndex++;
		}
		
//		System.out.println(ret);
		for (ArrayList<String> al : ret) {
			System.out.println(al.get(0)+","+al.get(1));
		}
	}
	
	public void print(Map<String, ArrayList<String>> map){
		for (Entry<String, ArrayList<String>> entry : map.entrySet()) {
			 String key = entry.getKey();
			 ArrayList<String> value = entry.getValue();
		    System.out.println(key + "->" + value);
		}
		
		
//		Iterator it = map.entrySet().iterator();
//	    while (it.hasNext()) {
//	        Map.Entry pairs = (Map.Entry)it.next();
//	        System.out.println(pairs.getKey() + " = " + pairs.getValue());
//	    }
	}

}


你可能感兴趣的:(两道面试题解答之一 Trading Server Deadlocks)