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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T09:30:48+00:00 2026-05-12T09:30:48+00:00

I am applying my new found knowledge of threading everywhere and getting lots of

  • 0

I am applying my new found knowledge of threading everywhere and getting lots of surprises

Example:

I used threads to add numbers in an
array. And outcome was different every
time. The problem was that all of my
threads were updating the same
variable and were not synchronized.

  • What are some known thread issues?
  • What care should be taken while using
    threads?
  • What are good multithreading resources.
  • Please provide examples.

sidenote:
(I renamed my program thread_add.java to thread_random_number_generator.java🙂

  • 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-12T09:30:48+00:00Added an answer on May 12, 2026 at 9:30 am

    In a multithreading environment you have to take care of synchronization so two threads doesn’t clobber the state by simultaneously performing modifications. Otherwise you can have race conditions in your code (for an example see the infamous Therac-25 accident.) You also have to schedule the threads to perform various tasks. You then have to make sure that your synchronization and scheduling doesn’t cause a deadlock where multiple threads will wait for each other indefinitely.

    Synchronization

    Something as simple as increasing a counter requires synchronization:

    counter += 1;
    

    Assume this sequence of events:

    • counter is initialized to 0
    • thread A retrieves counter from memory to cpu (0)
    • context switch
    • thread B retrieves counter from memory to cpu (0)
    • thread B increases counter on cpu
    • thread B writes back counter from cpu to memory (1)
    • context switch
    • thread A increases counter on cpu
    • thread A writes back counter from cpu to memory (1)

    At this point the counter is 1, but both threads did try to increase it. Access to the counter has to be synchronized by some kind of locking mechanism:

    lock (myLock) {
      counter += 1;
    }
    

    Only one thread is allowed to execute the code inside the locked block. Two threads executing this code might result in this sequence of events:

    • counter is initialized to 0
    • thread A acquires myLock
    • context switch
    • thread B tries to acquire myLock but has to wait
    • context switch
    • thread A retrieves counter from memory to cpu (0)
    • thread A increases counter on cpu
    • thread A writes back counter from cpu to memory (1)
    • thread A releases myLock
    • context switch
    • thread B acquires myLock
    • thread B retrieves counter from memory to cpu (1)
    • thread B increases counter on cpu
    • thread B writes back counter from cpu to memory (2)
    • thread B releases myLock

    At this point counter is 2.

    Scheduling

    Scheduling is another form of synchronization and you have to you use thread synchronization mechanisms like events, semaphores, message passing etc. to start and stop threads. Here is a simplified example in C#:

    AutoResetEvent taskEvent = new AutoResetEvent(false);
    
    Task task;
    
    // Called by the main thread.
    public void StartTask(Task task) {
      this.task = task;
      // Signal the worker thread to perform the task.
      this.taskEvent.Set();
      // Return and let the task execute on another thread.
    }
    
    // Called by the worker thread.
    void ThreadProc() {
      while (true) {
        // Wait for the event to become signaled.
        this.taskEvent.WaitOne();
        // Perform the task.
      }
    }   
    

    You will notice that access to this.task probably isn’t synchronized correctly, that the worker thread isn’t able to return results back to the main thread, and that there is no way to signal the worker thread to terminate. All this can be corrected in a more elaborate example.

    Deadlock

    A common example of deadlock is when you have two locks and you are not careful how you acquire them. At one point you acquire lock1 before lock2:

    public void f() {
      lock (lock1) {
        lock (lock2) {
          // Do something
        }
      }
    }
    

    At another point you acquire lock2 before lock1:

    public void g() {
      lock (lock2) {
        lock (lock1) {
          // Do something else
        }
      }
    }
    

    Let’s see how this might deadlock:

    • thread A calls f
    • thread A acquires lock1
    • context switch
    • thread B calls g
    • thread B acquires lock2
    • thread B tries to acquire lock1 but has to wait
    • context switch
    • thread A tries to acquire lock2 but has to wait
    • context switch

    At this point thread A and B are waiting for each other and are deadlocked.

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

Sidebar

Related Questions

Say you have a tuple and want to generate a new tuple by applying
I'm applying the following example http://jsoup.org/cookbook/extracting-data/example-list-links to list links. package org.jsoup.examples; import org.jsoup.Jsoup; import
Thing is, after applying a new color theme for my Flash Builder enviroment, the
I found applying the following function to a links onclose works to close Fancybox
I have no idea why this is only applying to the last instance found,
I am thinking of applying a new page to the admin section on my
When applying a multi-project Gradle structure to our project, my settings.gradle looks like this:
When applying the MVP pattern to ASP.NET applications, where does using AJAX to post
I was applying indexing to a fresh database(no records on most of the tables)
i am applying a border to NSView but how can i change the bordercolor.

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.