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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T17:57:58+00:00 2026-06-10T17:57:58+00:00

Given: public class Thread1 { int x = 0; public class Runner implements Runnable

  • 0

Given:

public class Thread1 {
int x = 0;

public 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 void go() {
    Runnable r1 = new Runner();

    new Thread(r1).start();
    new Thread(r1).start();


}

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

}

Which two are possible results? (Choose two)

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,

I chose A and B but I’m not certain if that is the correct answer.

  • 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-10T17:58:00+00:00Added an answer on June 10, 2026 at 5:58 pm

    First of all D. and E. are out. As i scope local to the function, we know that there is going to be 8 numbers.

    Now

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

    is incorrect. Why? In order print the last two positions each thread has to write the variable x at least two times. At this point the minimum value for x is 4. Which means that whatever the race conditions, the last two values should be greater or equal to 4.

    EDIT
    C is totally possible. Absence of synchronization doesn’t mean it is not possible to have what is called a serial execution (ie as if threads execute one after another). The lesson from concurrency is that you don’t know how threads will be interleaved

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

    happens for example :

    • when the first thread as time to finish updating x before the second start
    • when the first thread is suspended before the first current = x;, the second finish executing and the first continue.
    • tons of other cases.

    As Brian Goetz said:

    “Writing correct programs is hard; writing correct concurrent programs is harder.”

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

Sidebar

Related Questions

Given this code: public class Messager implements Runnable { public static void main(String[] args)
Given: public interface IBatchProcess { void Run(); } and multiple implementation of: public class
Given: public class TestSeven extends Thread { private static int x; public synchronized void
Given the class: public class Item { [Key] public int ID { get; set;
Given the following object: public class Product { string Name {get;} int Quantity {get;}
Given public class Original { public static DoStuff() { } } public class Derived
Given this class public partial class Default : Page { private IRepository repo; ...
given this class definition: public class Frame { IFrameStream CapturedFrom; } I want implement
Given a simple class: public class Person { public string FirstName; public string LastName;
Given a class containing an enum: public class MyClass { public enum NestedEnum {

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.