运行方法外的同步方法 [英] Synchronized method outside the run method

查看:77
本文介绍了运行方法外的同步方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此问题是对此问题的跟进,类似的问题但执行是与下面的代码不同,我对该对象没有任何锁定.因此,要想清楚地理解,我是对的.

This question is follow up on this Question, Similar question but the execution is different, As In the below code, I don't have any lock on the object. So trying to understand clearly, I am right or not.

到目前为止,我通过阅读书籍和文章了解的内容:-

每个线程都将输入run method,并根据if, else if block获得id from the various pool (existPool or newPool),然后它将进入attributeMethod,必须为synchronized,对吗?还有attributeMethod中的另一种方法,不需要同步吗?

Each Thread will enter the run method, and will get the id from the various pool (existPool or newPool) depending on if, else if block, then it will go into attributeMethod which has to be synchronized right? And there is another method in that attributeMethod which doesn't needs to be synchronized right?

因此,假设第二个线程也同时启动,那么下面的示例将有任何问题吗?

So suppose if second thread also launch at the same time, so I will be having any problem with the below example?

private static final class Task implements Runnable {

    private BlockingQueue<Integer> existPool;
    private BlockingQueue<Integer> newPool;
    private int existId;
    private int newId;
    private Service service;

    public Task(Service service, BlockingQueue<Integer> pool1, BlockingQueue<Integer> pool2) {
        this.service = service;
        this.existPool = pool1;
        this.newPool = pool2;
    }
    public void run()  {
      if(service.getCriteria.equals("Previous")) {
          existId = existPool.take();
          attributeMethod(existId);
        } else if(service.getCriteria.equals("New")) {
            newId = newPool.take();
            attributeMethod(newId);
        }
    }
}


    // So I need to make this method synchronized or not? Currently I have made this synchronized
    private synchronized void attributeMethod(int range) {
        // And suppose If I am calling any other method here-

         sampleMethod();
    }

    // What about this method, I don't thinkg so, it will be synchronized as well as it will be in the scope of previous synchronized method whoever is calling, Right? or not?
    private void sampleMethod() {


    }

推荐答案

因此,假设第二个线程也同时启动,那么下面的示例将有任何问题吗?

So suppose if second thread also launch at the same time, so I will be having any problem with the below example?

可能的,你会的.重读我对上一个问题的回答中的第二个要点.

Potentially, yes you will. Reread the second bullet point in my answer to your previous question.

基本上,问题在于线程将各自在Task类的不同实例上进行同步...并且不会提供任何互斥.

Basically, the problem is that the threads will each synchronize on a different instance of the Task class ... and that won't provide any mutual exclusion.

这是否是实际上,这里的问题将取决于线程是否需要 进行同步.在这种情况下,线程似乎将共享ServiceBlockingQueue实例.如果这是它们共享的程度,并且您正在使用线程安全的实现类,则可能不需要同步 .

Whether this is actually a problem here will depend on whether the threads need to synchronize. In this case, it appears that the threads will be sharing Service and BlockingQueue instances. If that is the extent of their sharing AND you are using thread-safe implementation classes, then synchronization may not be necessary.

我对您的建议是回到您的Java教科书/教程中,并回顾他们对synchronized和原始互斥锁实际上所做的事情的看法.它们确实非常简单……但是您需要充分理解这些基元,然后才能正确地将它们组合在一起,以实现您要实现的目标.

My advice to you would be go back to your Java textbook(s) / tutorial(s), and review what they say about what synchronized and primitive mutexes actually do. They are really quite simple ... but you need to fully understand the primitives before you can put them together correctly to achieve the goal you are trying to achieve.

这篇关于运行方法外的同步方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆