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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T00:19:28+00:00 2026-05-17T00:19:28+00:00

I’m prepping for the SCJP, and multithreading has been my shakiest area, mostly because

  • 0

I’m prepping for the SCJP, and multithreading has been my shakiest area, mostly because I don’t know how to look at multithreading code and walk through it. So far my approach has been to write down in English what might be happening in each thread and testing a few cases with threads randomly intersecting each other, which is a really hit-and-miss and time-consuming approach. So I’d like to see how a pro would go about it. Would you be willing to read the code below (it’s the latest question that’s giving me trouble) and write down what goes through your head (code-related stuff only, please 🙂 as you work out the possible outputs? The choices that come with the question are at the end. What I’m looking for isn’t the solution, which I have, but how one arrives at the solution efficiently on the exam.

And yeah, I know this question doesn’t have a precise answer, etc etc. Accepted vote goes to the answer that’s clearest and easiest to emulate, okay 🙂

Thanks everyone!

Question: Which of these answers are possible outputs?

public class Threads1 {

    int x = 0;

    class Runner implements Runnable {

        public void run() {
            int current = 0;
            for (int i = 0; i < 4; i++) {
                current = x;
                System.out.print(current + ", ");
                x = current + 2;
            }
        }
    }

    public static void main(String[] args) {
        new Threads1().go();
    }

    public void go() {
        Runnable r1 = new Runner();
        new Thread(r1).start();
        new Thread(r1).start();
    }
}

Choices (choose all that apply):

A. 0, 2, 4, 4, 6, 8, 10, 6,

B. 0, 2, 4, 6, 8, 10, 2, 4,

C. 0, 2, 4, 6, 8, 10, 12, 14,

D. 0, 0, 2, 2, 4, 4, 6, 6, 8, 8, 10, 10, 12, 12, 14, 14,

E. 0, 2, 4, 6, 8, 10, 12, 14, 0, 2, 4, 6, 8, 10, 12, 14,

  • 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-17T00:19:29+00:00Added an answer on May 17, 2026 at 12:19 am

    A and C (assuming the question is Which of these answers are possible outputs?)

    The hard part, of course, isn’t when you find a possible solution. But rather, its to look hard at the ones that you think are not possible and try to convinced yourself that you’ve got a solid reason why its not possible and that you’ve eliminated all the ways to get around your reason.

    So far my approach has been to write down in English what might be happening in each thread …

    You need to figure out which thread printed each number. Below is the most efficient, succinctness format I could think of represent that and make it easy to crossout/erase/write-over as you work through possibilities. Realize:

    • Once you find an possible answer move on. It doesn’t matter if it isn’t likely in the real world or that there may be other possible (or impossible) combinations. As long as you found 1 possibility, that’s all you need to move on.

    • Try the simplest approach first, e.g. assume T1 for each number until you hit a number that couldn’t be T1, so you fill in T2, and so on.. Hopefully, you get to the end with no contradiction (or contradictions that are easy to resolve). Once you’ve found a possible combination, move on.

    • Feel free to skip around to eliminate the possible ones quickly so you can focus on the likely impossible ones.

    Here is the final edit of my scratch-paper/worksheet (appended with my mental annotations):

    A. 0, 2, 4, 4, 6, 8, 10, 6,
       1  1  1  2  2  2   2  1     <- possible threads that produced this output - possible solution
    
    B. 0, 2, 4, 6, 8, 10, 2, 4,
       1  2  2  2  2   ?  1        <- to print second '2', T1 interrupted between L10/L11; 4 passes of T2 used up
    
    C. 0, 2, 4, 6, 8, 10, 12, 14,
       1  1  1  1  2   2   2   2   <- possible solution - simplest solution (T2 waits until T1 is completely done) - doesn't matter that it isn't likely, just that is possible
    
    D. 0, 0, 2, 2, 4, 4, 6, 6, 8, 8, 10, 10, 12, 12, 14, 14,
       1  2  1  2  1  2  1  2  1  2   ?    <- threads used up
    
    E. 0, 2, 4, 6, 8, 10, 12, 14, 0, 2, 4, 6, 8, 10, 12, 14,
       1  1  1  1  2   2   2   2  ?   <- threads used up
    

    Note:

    http://download.oracle.com/javase/tutorial/essential/concurrency/atomic.html

    • Reads and writes are atomic for reference variables and for most primitive variables (all types except long and double).
    • …

    Atomic actions cannot be interleaved, so they can be used without fear of thread interference.

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

Sidebar

Related Questions

I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I want to count how many characters a certain string has in PHP, but
I have a jquery bug and I've been looking for hours now, I can't
Basically, what I'm trying to create is a page of div tags, each has
I've got a string that has curly quotes in it. I'd like to replace
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I have a text area in my form which accepts all possible characters from
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function

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.