BasicPlayerEventLauncher.java :  » Music » yoyoplayer » com » hadeslee » yoyoplayer » player » Java Open Source

Java Open Source » Music » yoyoplayer 
yoyoplayer » com » hadeslee » yoyoplayer » player » BasicPlayerEventLauncher.java
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.hadeslee.yoyoplayer.player;

import java.util.HashSet;
import java.util.LinkedList;
import java.util.Set;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 * ,
 * 
 * @author hadeslee
 */
public class BasicPlayerEventLauncher extends Thread {

    private Set<BasicPlayerListener> listeners;//
    private LinkedList<BasicPlayerEvent> queue;//
    /**
     * ,,
     * ,BasicPlayer
     */
     BasicPlayerEventLauncher() {
        super("BasicPlayerEvent Dispacther Thread");
        listeners = new HashSet<BasicPlayerListener>();
        queue = new LinkedList<BasicPlayerEvent>();
    }

    /**
     * 
     * @param event 
     */
    public synchronized void put(BasicPlayerEvent event) {
        queue.offer(event);
        synchronized (this) {
            this.notifyAll();
        }
    }

    /**
     * 
     * @param listener 
     */
    public synchronized void addBasicPlayerListener(BasicPlayerListener listener) {
        listeners.add(listener);
    }

    /**
     * 
     * @return 
     */
    public synchronized Set<BasicPlayerListener> getBasicPlayerListeners() {
        return listeners;
    }

    /**
     * 
     * @param listener 
     */
    public synchronized void removeBasicPlayerListener(BasicPlayerListener listener) {
        listeners.remove(listener);
    }

    public void run() {
        while (true) {
            BasicPlayerEvent event = queue.poll();
            if (event == null) {//,
                synchronized (this) {
                    try {
                        this.wait();
                    } catch (InterruptedException ex) {
                        Logger.getLogger(BasicPlayerEventLauncher.class.getName()).log(Level.SEVERE, null, ex);
                    }
                }
            } else {//
                for (BasicPlayerListener bpl : listeners) {
                    bpl.stateUpdated(event);
                }
            }
        }

    }
}
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.