Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6716175
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T08:42:06+00:00 2026-05-26T08:42:06+00:00

How to synchronize two different methods from the same class in order to lock

  • 0

How to synchronize two different methods from the same class in order to lock on the same object? Here is an example:

public class MyClass extends Thread implements Observer{
  public List<AnotherClass> myList = null;
  
  public MyClass(List<AnotherClass> myList){
    this.myList = myList;
  }
  
  public void run(){
    while(true){
       //Do some stuff 
       myList.add(NotImportantElement);
    }
  }

  public void doJob{
    for(int i=0; i<myList.size; i++){
      ElementClass x = myList.get(i);
      //Do some more stuff
    }
  }
}

The question is how can I stop run() from accesing myList when doJob is executed and viceversa?

Imagine this: I start the thread and start adding elements to my list. At a random moment I call doJob() from another class that holds a reference to my thread.

How should I do the lock? Thanks!

L.E.

Ok, I understood the concept of the lock, but now I have another question.

Suppose I have a class with public static myList and only one instance of that class. From that instance I create n instances of Thread that take every element of that list and do some stuff with it.

Now, at a specific moment, myList is updated. What happens with those Threads that already were processing myList elements? How should I lock access on myList while updating it?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T08:42:06+00:00Added an answer on May 26, 2026 at 8:42 am

    NOTE: This code assumes you only have one instance of MyClass. according to your post that sounds like the case.

    public class MyClass extends Thread implements Observer{
      private List<AnotherClass> myList = null;
      private Object lock = new Object();
    
      public MyClass(List<AnotherClass> myList){
        this.myList = new ArrayList(myList);
      }
    
      public void run(){
        while(true){
           //Do some stuff 
           synchronized(lock) {
            myList.add(NotImportantElement);
           }
        }
      }
    
      public void doJob{
        synchronized(lock) {
          for(int i=0; i<myList.size; i++){
            ElementClass x = myList.get(i);
            //Do some more stuff
          }
        }
      }
    }
    

    EDIT: Added making a copy of List so that external entities could not change the list as per JB Nizet

    EDIT 2: Made variables private so nobody else can access them

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Consider the following code: public class Foo { private static final Object LOCK =
I have a WPF program with two listviews which display information from two different
I have two ViewModels that present the same Model to different Views. One presents
I need to synchronize a path animation on two different layers (one is used
I have two methods in a java class that both have a block of
I want to use rsync to synchronize two directories in both directions. I refer
I'm looking for a tool (best for free) which can synchronize two SQL Server
I’m using SSIS to synchronize data between two databases. I’ve used SSIS and DTS
hi i am newbie to javascript.... i want to synchronize scrolling of two select
The Java documentation says: it is not possible for two invocations of synchronized methods

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.