Is it safe to enable stateless lock calling in BlockingQueue?

I have the following pretty simple callback interface and POJO class:

public interface Action{
    public void doAction();
}

public class Person{
     private String name;
     private String address;
     //...etc
     //GET, SET, toString
}

And I will use it as follows:

public class ActionExecutor{

    private static final Logger logger = LogManager.getLogger(ActionExecutor.class);
    private final BlockingQueue<Action> blockingQueue = new LinkedBlockingQueue(2000);

    public void execute(final Person p){
        //modify state of p in some way
        blockingQueue.put(new Action(){
            public void doAction(){
                logger.info("Execution started: " +p.toString );
                //do some other job
        });
    }
}

BlockingQueue here it is used to implement the manufacturer-consumer.

Question: Is it guaranteed that the consumer flow taking measures out BlockingQueuewill write the correct journal message? That is, he observes the correct state Person? But I'm not so sure about that.

I think not, this is not guaranteed, because before the order between the changes made by the manufacturer and the reading by the producer does not occur.

+4
source share
1 answer

, .

Person , . , :

: , BlockingQueue - BlockingQueue .

. - Person , , . , , , , , , .

, :

public void execute(final Person p){
    //modify state of p in some way
    blockingQueue.put(new Action(){

, . p , new Action(), . - :

public void execute(final Person p){
    //modify state of p in some way
    blockingQueue.put(new Action() {
        final String executionName = p.toString();
        public void doAction(){
            logger.info("Execution started: " + executionName);
            //do some other job
    });
}

. , p .

+3

All Articles