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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T04:04:34+00:00 2026-06-17T04:04:34+00:00

Can anyone explain this code: public class Main implements Runnable{ private int i; public

  • 0

Can anyone explain this code:

public class Main implements Runnable{

private int i;
public synchronized void run() {
    System.out.print("i = "+ i +"\n");
    if (i % 5 != 0) {
        i++;
    }
    for (int x = 0; x < 5; x++, i++) {
        if (x > 1) {
            Thread.yield();
        }
    }
}

 public static void main(String[] args) {
    Main n = new Main();
    for (int x = 100; x > 0; --x) {
        new Thread(n).start();
    }
}
}

and how the output is

i= 0
i= 5
i= 10
i= 15
i= 20
.
.
.
i= 499
  • 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-17T04:04:36+00:00Added an answer on June 17, 2026 at 4:04 am

    The bottom loop creates 100 threads. Each thread is run immediately, in parallel with the others. In each thread,

    • at the start, the current value of shared-attribute ‘i’ is printed
    • if i is not divisible by 5, it is incremented once
    • then a loop is executed, up to 5 times. The last 3 times it is executed, it yields to other threads. Every time this loop executes, it increments shared-variable ‘i’

    Therefore, i is incremented 5 times if it was divisible by 5, and 6 times if it was not. But, the run() method is synchronized. Therefore, no other thread is allowed to execute while one of them is already executing: your program is effectively single-threaded, yield()s are ignored (no other thread can execute), and the output identical to

    for (int i=0; i<500; i+= 5) System.out.println(i);
    System.out.println(499);
    

    Remove the synchronized to see all sorts of race-conditions altering the output.

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

Sidebar

Related Questions

can anyone explain me why there is a deadlock in this code.Thanks public class
Can anyone explain this code to me? Whats the meaning and how to use
Can anyone explain why this code: {% form_theme form _self %} {% block avo_gallery_upload_widget
Can anyone explain why this code with the given routes returns the first route?
anyone can explain me, why this parts of code are acting differently? while((c =
This code crashes at the cout line. Can anyone explain why this doesn't work?
Given this code: class Overloading extends Object { static public void target(Object val, String
public class Asterisk { public static void main(String[] args) { String output=""; int count=1,
This is the sample code : public class OverloadingExample { public void display(Object obj){
Can anyone explain why this code gives the error: error C2039: 'RT' : is

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.