Hallo,
so jetzt habe ich mal eine Idee implementiert, die mir als lockfree Lock vorschwebt...
Hier wird auf Synchronisierung erzichtet sondern einfach solange in einer forschleife gewartet, bis
der Lock frei ist!
Ich habe es mit einer Counter-Impl getestet. Typisches Consumer/Producersystem... 2 Threads....
Der neue Lock hier ist bei mir 3-4 mal schneller! Habe einen Quadcore.
[code=Java]
package foxnet.system.util.concurrent.locks;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
/**
*
* @author Christopher Probst
*/
public class AtomicLock implements Lock {
private final AtomicBoolean busy = new AtomicBoolean(false);
private void internalLock() {
for (;;) {
if (tryLock()) {
return;
}
}
}
public void lock() {
//Acquire lock
internalLock();
}
public void lockInterruptibly() throws InterruptedException {
throw new UnsupportedOperationException("Not supported yet.");
}
public Condition newCondition() {
throw new UnsupportedOperationException("Not supported yet.");
}
public boolean tryLock() {
//Try to acquire the lock!
return busy.compareAndSet(false, true);
}
public boolean tryLock(long time, TimeUnit unit) throws InterruptedException {
throw new UnsupportedOperationException("Not supported yet.");
}
public void unlock() {
//We are not busy now
busy.set(false);
}
}
[/code]