Code example for CyclicBarrier

Methods: await

0
        return singerNameDictFed;
    } 
 
    public void await(CyclicBarrier barrier) {
        try { 
            barrier.await();
        } 
        catch (InterruptedException ie) {
            ie.printStackTrace();
        } 
        catch (BrokenBarrierException bbe) {
            bbe.printStackTrace();
        } 
    } 
 
    public void insertWord(String str, int id, NameIdsDict dict) {
        List<Integer> ids = null;
        str = str.trim();
        // 繁体字转成简体字,小写字母转成大写字母 
        str = Trad2Simp.convert(str).toUpperCase();
        if (!(dict.containsKey(str))) {
            ids = new ArrayList<Integer>();
        } 
        else { 
            ids = dict.get(str);
        } 
        ids.add(id);
        dict.put(str, ids);
    } 
 
	public void insertWord(String str, int id, NameIdsDict dict, NameIdsDict dictFed) {
		if (str != null) {
            if (!str.equals("")) {
                str = Preprocessor.rmAdditionalInfo(str);
                insertWord(str, id, dict);
			    str = Filter.process(str);
			    insertWord(str, id, dictFed); // 过滤处理后的key
            } 
		} 
	} 
 
	public void initDictFromDB(String sql, NameIdsDict dict, NameIdsDict dictFed) {
		try { 
            System.out.println(sql);
            // DriverManager.getConnection(MYSQL_URL, USER_NAME, PASSWORD); 
            Connection connection = H2Manager.getConnection();
			ResultSet resultSet = connection.createStatement().executeQuery(sql);
			while (resultSet.next()) {
                String name = resultSet.getString(2);
                int id = resultSet.getInt(1);
                System.out.println(name + " ===> " + id);
				insertWord(name, id, dict, dictFed);
			} 
			connection.close();
		} 
		catch (SQLException sqle) {
			sqle.printStackTrace();
		} 
	} 
 
    /** 
     * 初始化词典    大概 2 ~ 3 min 
     */ 
	public void initDict() { 
        // songs, singers 
        String[] tables = { "song", "singer" };
        int parties = tables.length + 1;
        CyclicBarrier barrier = new CyclicBarrier(parties);
 
        Thread thread = null;
        String sql = "";
		String format = "SELECT %s_id, %s_name FROM %ss";