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

  • SEARCH
  • Home
  • 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 8434541
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T06:41:05+00:00 2026-06-10T06:41:05+00:00

class ThreadSafe implements Runnable { int arr[]=new int[]{1,2,3,4,5}; int sum=0; public void run() {

  • 0
class ThreadSafe implements Runnable {
  int arr[]=new int[]{1,2,3,4,5};
  int sum=0;
  public void run() {
    int result=sum();
    System.out.println("for "+Thread.currentThread().getName()+"the value        is"+result);
  }

  public int sum() {
    for(int i=0;i<5;i++) {
      sum=sum+arr[i];
      System.out.println("calculating sum for      thread"+Thread.currentThread().getName()+"sum ="+sum);
      try {
        Thread.sleep(10);
      } catch(Exception e) {}
    }
    return sum;
  }

  public static void main(String...d) {
    ThreadSafe ts=new ThreadSafe();
    ThreadSafe ts1=new ThreadSafe();
    Thread t=new Thread(ts);
    Thread t1=new Thread(ts1);
    t1.start();
    t.start();
  }
}

I was expecting the output not to come 15. because the sum method is not synchronized so more then one thread can execute the sum method at the same time

What I was expecting that because the 2 thread’s will execute the sum method instantly so the output should not be 15 because the first thread will update the value of sum to some value which will be read by the another thread.

So my question is why the output of the program come out the way I’m expecting even though i haven’t synchronized the sum() method?

  • 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-06-10T06:41:06+00:00Added an answer on June 10, 2026 at 6:41 am

    You’re creating two instances of ThreadSafe, each with their own sum instance variable.

    If you want them to overwrite each other (I’m assuming this is just playing around), create two Threads on the same instance of ThreadSafe. Also, mark your sum variable as volatile to indicate that it can be changed by another thread (to avoid internal caching of the value if the compiler detects that it is not changed elsewhere within the method).

    For example, change your main method to this:

    public static void main(String...d) {
        ThreadSafe ts=new ThreadSafe();
        Thread t=new Thread(ts);
        Thread t1=new Thread(ts);
        t1.start();
        t.start();
      }
    

    And the beginning of your ThreadSafe class definition to this:

    class ThreadSafe implements Runnable {
      int arr[]=new int[]{1,2,3,4,5};
      volatile int sum=0;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

public class ThreadSafe implements ITaskCompletionListener { private final Set<String> taskIds = new HashSet<String>(); private
public class GetCurrentThread implements Runnable { Thread th; public GetCurrentThread(String threadName) { th =
How do I make the following class threadsafe? public class Helper { private static
I have a class that implements Runnable and am currently using an Executor as
// Not really how java.util.concurrent.Semaphore is implemented @ThreadSafe public class SemaphoreOnLock { private final
Suppose I have a class like this: package com.spotonsystems.bulkadmin.cognosSDK.util.Logging; public class RecordLogging implements LittleLogging{
I have a class with several data members: public class Foo { List<int> intLst;
First class: class Main1 { private ExecutorService service = Executors.newFixedThreadPool(4); public static void main(String[]
I am reading the book Java Concurrency in Practice session 4.3.5 @ThreadSafe public class
Does anyone know a quick and dirty threadsafe vector class for c++? I am

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.