import java.util.Arrays; public class TestCalc{ static String[] names = new String[] { "E", "B", "C", "B", "E", "C"}; static int index = 0; public static void main(String[] args) { for (int i = 0; i < names.length; i++) { Thread t = new Thread() { @Override public void run() { //Created thread from every search ThreadLocalUtils.DATA.set(this.getName()); // 用SQLSearch将数据查询的逻辑部分实现代码分离 System.out.println(new SQLSearch().run()); } }; t.setName(names[index++]); t.start(); } } public static class SQLSearch implements MyRunnable { public String run() { //返回查询结果 return search(ThreadLocalUtils.DATA.get()); } // 模拟查询 private String search(String name) { String result=null; // Sql if (name.equals("B")) { result = (name + "185 TALL"); } if (name.equals("C")) { result = (name + "160 TALL"); } if (name.equals("E")) { result = (name + "180 TALL"); } return result; } } public interface MyRunnable { public String run(); } public static class ThreadLocalUtils { public static final ThreadLocal<String> DATA = new ThreadLocal<String>(); } }