很多台DB server, 怎么确保其中一台挂了,里边的东西不会消失? 有多台server做replication
怎么知道刚刚消失的data在replication的哪边? 用多个pointer指每个server目前data的位址
很多人要读,偶尔写,要怎么保护? Reader-writer lock
如果一直不能轮到write怎么处理? 设一个duration,超过没轮到write,就禁止上读锁
cache用什么机制? lru, lfu
如果是做影音串流,cache用lru还是lfu好? lfu
lfu怎么做?
implement一个spin lock,
spin lock自旋锁:表示线程忙的锁
https://leetcode.com/discuss/interview-question/operating-system/134290/Implement-your-own-spinlock
public class SpinLock {
private AtomicBoolean lock;
public SpinLock() {
lock = new AtomicBoolean(false);
}
public void lock() {
while(!tryLock()){};
}
public boolean tryLock() {
return lock.compareAndSet(false, true);
}
public void unlock() {
lock.set(true);
}
}
实现wait¬ify
看着其实好像挺简单的
参考:https://howtodoinjava.com/java/multi-threading/wait-notify-and-notifyall-methods/
wait()
synchronized( lockObject )
{
while( ! condition )
{
lockObject.wait();
}
//take the action here;
}
notify()
synchronized(lockObject)
{
//establish_the_condition;
lockObject.notify();
//any additional code if needed
}
notifyAll()
synchronized(lockObject)
{
establish_the_condition;
lockObject.notifyAll();
}
然后创建Producer Thread,Consumer Thread
使用mutex(其实就是锁吧)
参考:https://crunchify.com/what-is-java-semaphore-and-mutex-java-concurrency-multithread-explained-with-example/
public class CrunchifySemaphoreMutexTutorial {
static Object crunchifyLock = new Object();
static LinkedList<String> crunchifyList = new LinkedList<String>();
// Semaphore maintains a set of permits.
// Each acquire blocks if necessary until a permit is available, and then takes it.
// Each release adds a permit, potentially releasing a blocking acquirer.
static Semaphore semaphore = new Semaphore(0);
static Semaphore mutex = new Semaphore(1);
// I'll producing new Integer every time
static class CrunchifyProducer extends Thread {
public void run() {
int counter = 1;
try {
while (true) {
String threadName = Thread.currentThread().getName() + counter++;
mutex.acquire();
crunchifyList.add(threadName);
System.out.println("Producer is prdoucing new value: " + threadName);
mutex.release();
// release lock
semaphore.release();
Thread.sleep(500);
}
} catch (Exception x) {
x.printStackTrace();
}
}
}
如何用纯c语言连接mongodb数据库进行读写操作?
参考:https://docs.mongodb.com/drivers/c/
#include <mongoc/mongoc.h>
int
main (int argc, char *argv[])
{
mongoc_database_t *database;
mongoc_client_t *client;
mongoc_init ();
client = mongoc_client_new(
"mongodb+srv://<username>:<password>@<cluster-url>/test?retryWrites=true&w=majority"
);
database = mongoc_client_get_database (client, "test");
mongoc_database_destroy (database);
mongoc_client_destroy (client);
mongoc_cleanup ();
return 0;
}