Cross Sharing ArrayLists or Strings

Discussion in 'Plugin Development' started by AKMiner98, Dec 8, 2013.

Thread Status:
Not open for further replies.
  1. Offline

    AKMiner98

    I want to edit a String or a ArrayList across 2 different classes. I have searched a little and found nothing, any suggestions?
     
  2. Offline

    Axe2760

    Learn java?
     
  3. Offline

    AKMiner98

    I am self teaching myself Java. And like I have said, I searched on how to do it. I posted this to get help, not for you to say some stupid remark.
     
  4. Offline

    Axe2760

    I mean what I say when I say learn java. What you need to understand are some of the fundamental concepts of java to share it across classes. Sorry, i am in a bad mood, i truly didn't mean to come off in a rude way.

    You can probably just use static fields/methods, or alternatively you can just keep an instance of the second class and make calls to.that when necessary
     
  5. Offline

    Not2EXceL

    Eww no static. Reference the instance of the class containing the list and modify that way.

    And you clearly need to learn the concept of objects then. Its fundamental
     
  6. Offline

    Iroh

    Moved to plugin development.
     
  7. Offline

    reider45

    I think I saw this on a youtube video once... Might help to check there
     
  8. Offline

    Wolfey

    I like to use a static getInstance and it returns the instance:
    Code:java
    1.  
    2. public class Class {
    3.  
    4. private Class() {}
    5.  
    6. static Class instance = new Class();
    7.  
    8. public static Class getInstance() {
    9. return instance;
    10. }
    11.  
    12. public void addToList(Player p) {
    13. list.add(p.getName());
    14. }
    15.  
    16. // other methods
    17. }
    18.  

    Then just do Class.getInstance().addToList(player);
     
  9. Offline

    Cirno


    Remember to set instance to null when you're done using it; e.g onDisable, or else you'll have some memory leaks.
     
  10. Offline

    Not2EXceL


    this is probably the worst form a singleton can take. (edit im being tired, and not mentioning some important things). default modifier != private. However plugins have very rare usages for singletons
     
  11. Offline

    Wolfey

    Yea I know, I don't use it often, I like to do it like this:
    Code:java
    1.  
    2. public class Class {
    3.  
    4. private static volatile Class instance = null;
    5.  
    6. private Class() {}
    7.  
    8. public static Class getInstance() {
    9. if (instance == null) {
    10. synchronized (Class.class) {
    11. if (instance == null) {
    12. instance = new Class();
    13. }
    14. }
    15. }
    16. return instance;
    17. }
    18. }
    19.  

    Learned it a long time ago, have been using it ever since for Manager classes and such.
     
  12. Offline

    Not2EXceL


    But you don't always use a double lock. It very depends on what your class is used for, after the intial singleton usage determination. Sometimes making an early binding is useful, but not often. Most times a singleton is useless or bad practice.

    Do you know why the double locking and synchronization exists?
     
  13. Offline

    xTrollxDudex

    Ehem, me...

    Also, you're using some big terms there, mind explaining "double-locking"? I vaguely understand synchronization and synchronized(...) but do explain that too.
     
  14. Offline

    Wolfey

    "Synchronized methods enable a simple strategy for preventing thread interference and memory consistency errors."

    But now that I'm looking at the code, I'm starting to realize that I probably should remove the double checked locking, since threads A and B are trying to assign memory space to instance, which is going to cause B to fail because it is using a partially constructed version of instance.
     
  15. Offline

    Not2EXceL

    No you need to keep the double lock if you're going to use a late binding for the singleton. Otherwise you can cause multi-threading problems. Theres a reason why its there.

    xTrollxDudex
    Double locking is simply short term for double checked locking optimization. For the snippet it refers to the 2 checks for null, one non-synced, the other synced.
     
  16. Offline

    xTrollxDudex

    Not2EXceL
    And synchronized? What exactly does it do?
     
  17. Offline

    Not2EXceL

    You mean the keyword? or synchronization in general?
     
  18. Offline

    xTrollxDudex

    Not2EXceL
    If you could explain both that would be fine
     
  19. Offline

    Not2EXceL

    In simpler terms, synchronization is used to prevent errors when multiple threads access the same fields and or objects. It pauses access till the first thread is finished, then continues the rest of the threads one by one. The word synchronized is to declare a method or statement synchronized (also deciding where the lock comes from).

    A simple example would be have 2 threads run and edit a single field accordingly, without synchronization, and a second time with synchronization.

    Note: This is a simplified explanation. IF you wish to learn more and how you can appropriately apply synchronization, lookup concurrency.
     
  20. Offline

    The_Doctor_123

    Not2EXceL
    Adding onto that, there is a another keyword in Java relating to concurrency called "volatile." Applying the modifier to fields will ensure that all Threads will receive up-to-date values on that field. Not using that keyword on a field with multiple Threads reading/modifying it may result in getting a cached value.
     
  21. Offline

    Not2EXceL

    The_Doctor_123 Yeah thanks. When simplifying I tend to leave some things out. There's also atomic variables, but that's for another discussion.
     
  22. Offline

    The_Doctor_123

    Not2EXceL
    :confused: Learn something new every day.. never heard of these "atomic variables." Although, I'd rather use synchronized and volatile in combination than this thing you're talking about.
     
  23. Offline

    Not2EXceL

    The_Doctor_123
    Real simple explanation of Atomic Variables. They minimize synchronization to prevent errors. However using most primitive variables or declaring variables volatile also creates atomic access. It delves deeper into concurrency.
     
  24. Offline

    The_Doctor_123

    Not2EXceL
    I know, I looked at the JavaDocs. It would just seem like your code would be easier to read by making methods synchronized and fields volatile. Just a preference, I suppose. Not sure if there's any performance gain, however.
     
Thread Status:
Not open for further replies.

Share This Page