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 741549
In Process

The Archive Base Latest Questions

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

consider this class,with no instance variables and only methods which are non-synchronous can we

  • 0

consider this class,with no instance variables and only methods which are non-synchronous can we infer from this info that this class in Thread-safe?

public class test{

public void test1{

// do something

}

public void test2{

// do something


}

public void test3{

// do something

}



}
  • 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-14T08:38:42+00:00Added an answer on May 14, 2026 at 8:38 am

    Not being thread safe means that if multiple threads try to access the object at the same time, something might change from one access to the next, and cause issues. Consider the following:

    int incrementCount() {
        this.count++;
        // ... Do some other stuff
        return this.count;
    }
    

    would not be thread safe. Why is it not? Imagine thread 1 accesses it, count is increased, then some processing occurs. While going through the function, another thread accesses it, increasing count again. The first thread, which had it go from, say, 1 to 2, would now have it go from 1 to 3 when it returns. Thread 2 would see it go from 1 to 3 as well, so what happened to 2?

    In this case, you would want something like this (keeping in mind that this isn’t any language-specific code, but closest to Java, one of only 2 I’ve done threading in)

    int incrementCount() synchronized {
        this.count++;
        // ... Do some other stuff
        return this.count;
    }
    

    The synchronized keyword here would make sure that as long as one thread is accessing it, no other threads could. This would mean that thread 1 hits it, count goes from 1 to 2, as expected. Thread 2 hits it while 1 is processing, it has to wait until thread 1 is done. When it’s done, thread 1 gets a return of 2, then thread 2 goes throguh, and gets the expected 3.

    Now, an example, similar to what you have there, that would be entirely thread-safe, no matter what:

    int incrementCount(int count) {
        count++;
        // ... Do some other stuff
        return this.count;
    }
    

    As the only variables being touched here are fully local to the function, there is no case where two threads accessing it at the same time could try working with data changed from the other. This would make it thread safe.

    So, to answer the question, assuming that the functions don’t modify anything outside of the specific called function, then yes, the class could be deemed to be thread-safe.

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

Sidebar

Related Questions

Consider this - a base class A, class B inheriting from A, class C
Consider this HashMap extention (generates an instance of the V class when calling get
Consider this class hierarchy: Book extends Goods Book implements Taxable As we know, there
Consider this: class User < ActiveRecord::Base # name, email, password end class Article <
Consider this class: class test { public function __set($n, $v) { echo __set() called\n;
Consider this sample class, class TargetClass { private static String SENSITIVE_DATA = sw0rdfish; private
Consider this code : class MyClass<T> { } class AnotherClass : MyClass<String> { }
Let's consider this model class Session(models.Model): tutor = models.ForeignKey(User) start_time = models.DateTimeField() end_time =
Consider this simple AS3 class. package { import flash.display.Sprite; import flash.display.MovieClip; public class MySprite
Consider this simple example - public class Person { private String name; private Date

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.