-
Notifications
You must be signed in to change notification settings - Fork 76
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
4f35937
commit 85658ab
Showing
3 changed files
with
104 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 ©); // Not Implemented | ||
MySingleton &operator=(const MySingleton ©); // 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); | ||
|