当前位置: 首页 > 工具软件 > Hazelcast > 使用案例 >

【Hazelcast】Lock

赫连卓
2023-12-01

加锁解锁

package com.cqh.Hazelcast_Lock;

import com.hazelcast.core.Hazelcast;
import com.hazelcast.core.HazelcastInstance;

import java.util.concurrent.locks.Lock;


/**
 * Created by yl1794 on 2018/5/2.
 */
public class Test_Lock {
    public static void main(String[] args){
        HazelcastInstance hazelcastInstance = Hazelcast.newHazelcastInstance();
        Lock lock = hazelcastInstance.getLock("myLock");
        lock.lock();
        try {
            // do something here
            System.out.println("do something here");
        } finally {
            lock.unlock();
        }
    }
}

使用tryLock释放锁

package com.cqh.Hazelcast_Lock;

import com.hazelcast.core.Hazelcast;
import com.hazelcast.core.HazelcastInstance;

import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.Lock;

/**
 * Created by yl1794 on 2018/5/2.
 */
public class Test_TryLock {
    public static void main(String[] args) throws InterruptedException {
        HazelcastInstance hazelcastInstance = Hazelcast.newHazelcastInstance();
        Lock lock = hazelcastInstance.getLock("myLock");
        if( lock.tryLock(10, TimeUnit.SECONDS) ){
            try {
                // do something here
                System.out.println("do something here");
            }finally {
                lock.unlock();
            }
        }else {
            // warning
            System.out.println("warning");
        }
    }
}

使用租赁时间释放锁

package com.cqh.Hazelcast_Lock;

import com.hazelcast.core.Hazelcast;
import com.hazelcast.core.HazelcastInstance;
import com.hazelcast.core.ILock;

import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.Lock;

/**
 * Created by yl1794 on 2018/5/2.
 */
public class Test_Lock_LeaseTime {
    public static void main(String[] args){
        HazelcastInstance hazelcastInstance = Hazelcast.newHazelcastInstance();
        ILock lock = hazelcastInstance.getLock("myLock");
        lock.lock( 5, TimeUnit.SECONDS );
        try {
            // do something here
            System.out.println("do something here");
        } finally {
            try {
                lock.unlock();
            } catch ( IllegalMonitorStateException ex ){
                // WARNING Critical section guarantee can be broken
            }
        }
    }
}

使用ICondition同步线程(生产者)

package com.cqh.Hazelcast_Lock;

import com.hazelcast.core.Hazelcast;
import com.hazelcast.core.HazelcastInstance;
import com.hazelcast.core.ICondition;
import com.hazelcast.core.ILock;

import java.util.concurrent.locks.Lock;

/**
 * Created by yl1794 on 2018/5/2.
 */
public class Test_ICondition_Producer {
    public static void main(String[] args) throws InterruptedException {

        HazelcastInstance hazelcastInstance = Hazelcast.newHazelcastInstance();
        ILock lock = hazelcastInstance.getLock( "myLockId" );
        ICondition condition = lock.newCondition( "myConditionId" );

        lock.lock();
        try {
            while ( !shouldProduce() ) {
                condition.await(); // frees the lock and waits for signal
                // when it wakes up it re-acquires the lock
                // if available or waits for it to become
                // available
            }
            produce();
            condition.signalAll();
        } finally {
            lock.unlock();
        }

    }

    private static void produce() {
        System.out.println("produce");
    }

    private static Boolean shouldProduce(){
        return null;
    }


}

使用ICondition同步线程(消费者)

package com.cqh.Hazelcast_Lock;

import com.hazelcast.core.Hazelcast;
import com.hazelcast.core.HazelcastInstance;
import com.hazelcast.core.ICondition;
import com.hazelcast.core.ILock;

/**
 * Created by yl1794 on 2018/5/2.
 */
public class Test_ICondition_Consumer {
    public static void main(String[] args) throws InterruptedException {
        HazelcastInstance hazelcastInstance = Hazelcast.newHazelcastInstance();
        ILock lock = hazelcastInstance.getLock( "myLockId" );
        ICondition condition = lock.newCondition( "myConditionId" );

        lock.lock();
        try {
            while ( !canConsume() ) {
                condition.await(); // frees the lock and waits for signal
                                   // when it wakes up it re-acquires the lock
                                   // if available or waits for it to become
                                   // available
            }
            consume();
            condition.signalAll();
        } finally {
            lock.unlock();
        }


    }

    private static void consume() {
        System.out.println("consume");
    }

    private static Boolean canConsume(){
        return null;
    }
}

 类似资料: