I'm trying to implement at playlist in java. The only problem is that populatePlaylist() is a bit slow and needs to be locked. I have written this code but it doesn't seem to work.
private static final Lock lock = new ReentrantLock();
public Playlist(Context context, int length, boolean isLocked) {
lock.lock();
this.mContext = context;
this.populatePlaylist(length);
// If the playlist is locked we do not unlock it.
// wait for someone to do it manually.
if (!isLocked)
lock.unlock();
}
public void unlock() {
lock.unlock();
}
When the constructor is called the second time it just executes all the code even though unlock() has never been called.
EDIT: Here is what I try to do. The playlist class is just a representation of a playlist (surprise :) ). populate playlist may be a bit slow to generate so I share the playlist class between different parts of the program in order to avoid loading the playlist more than once. Yes, the playlist is only created once in every thread. One the UI mode, once it a background thread, once other places as well.
The playlist is static. Inside "populateplaylist" the code looks somewhat like this:
if (playlist.isEmpty()) { create_playlist } else {nothing}