Skip to content

Commit

Permalink
add design
Browse files Browse the repository at this point in the history
  • Loading branch information
fdong-coursera committed Jul 5, 2015
1 parent 4f35937 commit 85658ab
Show file tree
Hide file tree
Showing 3 changed files with 104 additions and 0 deletions.
28 changes: 28 additions & 0 deletions design/blocking_queue.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
public class BlockingQueueImpl<T> implements BlockingQueue<T>
{
private final Queue<T> q = new LinkedList<T>();
private final int maxSize;
private final Object mutex = new Object();

public BlockingQueueImpl(int maxSize) {
if (maxSize <= 0)
throw new IllegalArgumentException("maxSize=" + maxSize);
this.maxSize = maxSize;
}

public T take() throws InterruptedException {
synchronized (mutex) {
while (q.isEmpty()) mutex.wait();
mutex.notifyAll(); // Wake up threads waiting to put (or take)
return q.remove();
}
}

public void put(T obj) throws InterruptedException {
synchronized (mutex) {
while (q.size() >= maxSize) mutex.wait();
mutex.notifyAll(); // Wake up threads waiting to take (or put)
q.add(obj);
}
}
}
47 changes: 47 additions & 0 deletions design/requests.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
#include <time.h>
#include <sys/time.h>
long now() {
struct timeval time;
gettimeofday(&time, NULL);
return (time.tv_sec * 1000000 + time.tv_usec);
}

class HitCounter {
private:
deque<pair<long, int>> hits;
long last_count = 0;
const int second = 1000000;
const int minute = 60 * second;
const int hour = 60 * minute;

void prune() {
auto old = upper_bound(hits.begin(), hits.end(), make_pair(now() - 1 * hour, -1));
if (old != hits.end()) {
hits.erase(hits.begin(), old);
}
}

public:
void hit() {
hits.push_back(make_pair(now(), ++last_count));
prune();
}

long hitsInLastSecond() {
auto before = lower_bound(hits.begin(), hits.end(), make_pair(now() - 1 * second, -1));
if (before == hits.end()) { return 0; }
return last_count - before->second + 1;
}

long hitsInLastMinute() {
auto before = lower_bound(hits.begin(), hits.end(), make_pair(now() - 1 * minute, -1));
if (before == hits.end()) { return 0; }
return last_count - before->second + 1;
}

long hitsInLastHour() {
auto before = lower_bound(hits.begin(), hits.end(), make_pair(now() - 1 * hour, -1));
if (before == hits.end()) { return 0; }
return last_count - before->second + 1;
}
};
29 changes: 29 additions & 0 deletions design/singleton.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// Example for singleton pattern
// class definition
class MySingleton {
private:
// Private Constructor
MySingleton();
// Stop the compiler generating methods of copy the object
MySingleton(const MySingleton &copy); // Not Implemented
MySingleton &operator=(const MySingleton &copy); // Not Implemented
static MySingleton *m_pInstance;
public:
static MySingleton *getSharedInstance() {
if (!m_pInstance) {
m_pInstance = new MySingleton;
}
return m_pInstance;
}
};

// in the source file
MySingleton *MySingleton::m_pInstance = NULL;

注意本例中的实现方式针对非多线程的情况如果有过个线程想要同时调用getSharedInstance函数则需要用mutex保护下列代码
pthread_mutex_lock(&mutex);
if (!m_pInstance) {
m_pInstance = new MySingleton;
}
pthread_mutex_unlock(&mutex);

0 comments on commit 85658ab

Please sign in to comment.