Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/401.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何编写一个简单的公平锁?_Java_Multithreading - Fatal编程技术网

Java 如何编写一个简单的公平锁?

Java 如何编写一个简单的公平锁?,java,multithreading,Java,Multithreading,如何将一个简单的公平锁模拟写入newReentrantLock(true) 自定义不公平锁(我不确定它是否正确) 如果您想要一个公平锁,您需要使用一个列表,并按照列表顺序通知线程 public class Main1 { public static void main(String[] args) { // Lock lock = new ReentrantLock(true); CustomLock lock = new CustomLock();

如何将一个简单的公平锁模拟写入new
ReentrantLock(true)

自定义不公平锁(我不确定它是否正确)


如果您想要一个公平锁,您需要使用一个列表,并按照列表顺序通知线程

     public class Main1 {

    public static void main(String[] args) {
//      Lock lock = new ReentrantLock(true);
        CustomLock lock = new CustomLock();
        new Thread(new Producer(lock)).start();
        new Thread(new Consumer(lock)).start();
    }
}

class Producer implements Runnable {
    private Lock lock;
    private CustomLock customLock;

    public Producer(Lock lock) {
        this.lock = lock;
    }

    public Producer(CustomLock lock) {
        this.customLock = lock;
    }

    @Override
    public void run() {
        while (!Thread.currentThread().isInterrupted()) {
//          lock.lock();
            customLock.lock();
            System.out.println("Producer before");
            try {
                TimeUnit.SECONDS.sleep(1);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("Producer after");
//          lock.unlock();
            customLock.unlock();
        }
    }
}

class Consumer implements Runnable {
    private Lock lock;
    private CustomLock customLock;

    public Consumer(Lock lock) {
        this.lock = lock;
    }

    public Consumer(CustomLock lock) {
        this.customLock = lock;
    }

    @Override
    public void run() {
        while (!Thread.currentThread().isInterrupted()) {
//          lock.lock();
            customLock.lock();
            System.out.println("Consumer before");
            try {
                TimeUnit.SECONDS.sleep(1);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("Consumer after");
//          lock.unlock();
            customLock.unlock();
        }
    }
}

class CustomLock{
    private boolean isLocked;

    public synchronized void lock(){
        while (isLocked) {
            try {
                wait();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        isLocked = true;
    }

    public synchronized void unlock(){
        if(isLocked){
            isLocked = false;
            notify();
        }
    }
}
class CustomLock{
    private boolean isLocked;

    public synchronized void lock(){
        while (isLocked) {
            try {
                wait();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        isLocked = true;
    }

    public synchronized void unlock(){
        if(isLocked){
            isLocked = false;
            notify();
        }
    }
}